Remember to

  • download portal_mammals.sqlite.
  • make sure the copy you are going to use in class does not have the SpeciesCounts table or view.

Installation

install.packages(c("DBI", "dbplyr", "RSQLite"))

Connect

library(dplyr)
portaldb <- src_sqlite("portal_mammals.sqlite")

Check out database structure

portaldb
src_tbls(portaldb)
tbl(portaldb, "plots")

surveys <- tbl(portaldb, "surveys") %>% collect()
surveys
colnames(surveys)

Write a query

count_query <- "SELECT genus, species, COUNT(*)
                FROM surveys
                JOIN species
                USING (species_id)
                GROUP BY genus, species"

tbl(portaldb, sql(count_query))

Do Exercise 1 - Source and Query.

Do Exercise 2 - Manipulate Query.

Using dplyr with databases

surveys <- tbl(portaldb, "surveys")
surveys
species <- tbl(portaldb, "species")
species_counts <- inner_join(surveys, species, by = "species_id") %>%
               group_by(genus, species) %>%
               summarize(count = n())

Do Links to Databases.

Move the final data into R

species_counts <- inner_join(surveys, species, by = "species_id") %>%
               group_by(genus, species) %>%
               summarize(count = n()) %>%
               collect()

Write new information to database

Show species_counts table NOT in portal_mammals.sqlite.

copy_to(portaldb, species_counts, temporary=FALSE, 
        name="SpeciesCounts")
portaldb

Show SpeciesCounts table in portal_mammals.sqlite with new name.

Do Copy to Database.