How to use R to view and fix records in the Nestwatch database


This document will take you through the process of modifying database records in R. A couple of things to note:


Set-up


We need to start by installing packages that you may not currently have. To do so, run the code below on your computer. Run one line at a time, as some lines may require your input and you will need to know if any errors are generated. Note: This will only have to be done once and will not have to be repeated each time you want to modify or add records.

install.packages('Rtools')

install.packages('devtools')

install.packages('httpuv', dependencies = TRUE)

install.packages('stringi')

install.packages('shiny', dependencies = TRUE)

install.packages('mongolite')

install.packages('tidyverse')

install.packages('editData')

install.packages('RCurl')

The next step may seem frightening, but it’s not – you don’t have to understand what this stuff does. This is the behind-the-scenes mechanics that makes reading and writing to the database possible. Simply copy-and-paste this section into R and run the entire section.

# Load libraries:

library(RCurl)

# Load a source script:

script <-
  getURL(
    "https://raw.githubusercontent.com/SMBC-NZP/neighborhoodNestwatch/master/fix_mongoRecords.R?token=AFXm5FJQxCUOPDRUOelb7lRc7jfryR-Sks5bNitGwA%3D%3D"
  )


# Evaluate then remove the source script:

eval(parse(text = script))

rm(script)

Reading records


The database is made up of a series of collections. Collections can be thought of as tables. The collections that are a part of the database include:

Get all records in a collection:To read a collection from the database and view it as a table, you would use the function getMongo with the name of the collection (in quotes) within the parentheses:

get_mongoData(
  collection  = 'staffContacts'
  )

Get collection data for a regional hub: If you want to read the collection information associated with a single regional hub, you can simply add the hub name as an argument of the get_collection function. Hubs include:

get_mongoData(
  collection  = 'staffContacts',
  hub = 'DC'
  )

Get collection data for a site: If you want to read the collection information associated with a single site, include arguments for the name of the collection and the name of the site, both in quotes. Note: Not all collections have a site field!

get_mongoData(
  collection  = 'visits',
  site = 'REITBOBMD1'
  )

Get collection data for a site and date: If you want to read the collection information associated with the date that a site was visited, add the arguments for the name of the collection, the name of the site, and the observation date to the function. Collections with site and date information include:

get_mongoData(
  collection  = 'captures',
  site = 'REITBOBMD1',
  date = '2000-06-07'
  )

How to save records to an Excel-friendly file format


You can save records to .csv files if you would prefer to play with them in Excel. To do so, you would use the save_records function and include the name of the collection.

save_records(
  collection = 'visits'
  )

Records will be saved to your current working directory. You can use the getwd function to find out where that is.

getwd()

You can also save records associated with a single hub, site, or visit, as follows.

# Save records associated with a single hub:

save_records(
  collection = 'captures',
  hub = 'DC'
  )

# Save records associated with a single site:

save_records(
  collection = 'captures',
  site = 'REITBOBMD1'
  )

# Save records associated with a single visit:

save_records(
  collection = 'captures',
  site = 'REITBOBMD1',
  date = '2000-06-07'
  )

Adding and modifying data


You will be happy to know that there is now a GUI for adding and modifying data! To use the gui to add data, you use the function editMongo. Most of the arguments of this function are the same as reading the data above. The only additional argument is newData. If newData = TRUE (the default), then new records will be added to the database.

# Add a new record for a site:

editMongo(
  collection = 'captures',
  site = 'test',
  newData = TRUE
  )

To modify an existing record, you simply modify the newData argument such that newData = FALSE.

# Modify data for all dates associated with a site:

editMongo(
  collection = 'captures',
  site = 'test',
  newData = FALSE
  )

# Modify data for a given date associated with a site:

editMongo(
  collection = 'captures',
  site = 'test',
  date = '2018-05-17',
  newData = FALSE
  )

That’s it!