knitr::opts_chunk$set( collapse = TRUE, eval = FALSE, echo = TRUE, comment = "#>" )
create_ybt_database.R
script in the inst/
subfolder of the ybt
package. ybt
package and in the RSQLite
packageybt::ybt_db_append
function is used to append all tables except for new deployments (so, tags, chn, wst, and detections)ybt::write_deployments
function is used to add new deployment records to the deployment table./inst/
).R
.dbDisconnect(connection)
.ybt::ybt_db_append()
function.ybt::ybt_db_append()
function.ybt::write_deployments()
function.The detections are (of course) the hardest part of this workflow, and so they get their own vignette, which you can bring up with vignette(topic = "Database-detections", package = "ybt")
.
The nice thing about doing all this in R
is that if you're already familiar with the dplyr
package, you can use it to construct and run database queries (please see this tutorial for more information). Some examples of common database queries are below, but all the queries you need for the Analysis workflow are pre-written into the scripts and reproducible report.
# database connection: requires >= RSQLite 2.1.1 db = RSQLite::dbConnect(RSQLite::SQLite(), "../yb_database.sqlite") # check relative path # get general info about database dbListTables(db) tbls = dbListTables(db)
If you already know basic SQL, I think the best way to get what you need is to use the dbGetQuery()
function from the RSQLite
package:
# how many rows per table? sapply(tbls, function(x) dbGetQuery(db, paste("SELECT COUNT(*) FROM" , x))) # how many/what fields per table? sapply(tbls, dbListFields, conn = db)
If you're more comfortable with the dplyr
package and tidyverse
workflows, you can access the database with the tbl()
function and then pipe through:
library(dplyr) # All tagging metadata (including both wst and chinook) alltags = tbl(db, "tags") %>% collect() %>% # this pulls the whole table mutate(DateTagged = as.Date(DateTagged)) # this gets the dates and times in the correct format # just white sturgeon: wst = tbl(db, "tags") %>% filter(Sp == "wst") %>% collect() # detections for just the white sturgeon tagged in 2012: wst2012 <- tbl(db, "tags") %>% # first get their TagID info inner_join(tbl(db, "detections"), by = "TagID") %>% # join it with the detections table filter(TagGroup == "wst_2012") %>% # filter down to just the 2012 white sturgeon collect() # bring it into R as a data frame
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.