diff --git a/.gitignore b/.gitignore index a7fd535..af0f3f4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.html Test.* data/ +data_raw/ diff --git a/prep/Preparation-RNotebook.ipynb b/prep/Preparation-RNotebook.ipynb index 46ab470..cffe8a0 100644 --- a/prep/Preparation-RNotebook.ipynb +++ b/prep/Preparation-RNotebook.ipynb @@ -41,7 +41,9 @@ "library(tidyverse)\n", "library(lubridate)\n", "library(ratdat)\n", - "library(ggplot2)" + "library(ggplot2)\n", + "\n", + "install.packages(c(\"dbplyr\", \"RSQLite\"))" ] }, { diff --git a/prep/Preparation-RStudio.Rmd b/prep/Preparation-RStudio.Rmd index 60b7465..90d41a8 100644 --- a/prep/Preparation-RStudio.Rmd +++ b/prep/Preparation-RStudio.Rmd @@ -102,3 +102,32 @@ Write Data ```{r} write_csv(surveys, "data/cleaned/test.csv") ``` + +## Databases + +Setup +```{r} +dir.create('data_raw', recursive = TRUE, showWarnings = FALSE) +install.packages(c("dbplyr", "RSQLite")) +library(dplyr) +library(dbplyr) +``` + +Download and connect to database +```{r} +download.file(url = "https://ndownloader.figshare.com/files/2292171", + destfile = "data_raw/portal_mammals.sqlite", mode = "wb") +mammals <- DBI::dbConnect(RSQLite::SQLite(), "data_raw/portal_mammals.sqlite") +src_dbi(mammals) +``` + +Run Queries +```{r} +tbl(mammals, sql("SELECT year, species_id, plot_id FROM surveys")) + +surveys <- tbl(mammals, "surveys") +surveys %>% + select(year, species_id, plot_id) + +head(surveys, n = 10) +``` diff --git a/prep/Setup.Rmd b/prep/Setup.Rmd index 5dc484c..911fc07 100644 --- a/prep/Setup.Rmd +++ b/prep/Setup.Rmd @@ -21,3 +21,8 @@ Packages from Lesson 1 ```{r message=FALSE, warning=FALSE} install.packages("ggplot2") ``` + +Packages from Data Lesson +```{r message=FALSE, warning=FALSE} +install.packages(c("dbplyr", "RSQLite")) +```