-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy path01_266 dbplyr R to SQL.r
More file actions
40 lines (29 loc) · 1.04 KB
/
01_266 dbplyr R to SQL.r
File metadata and controls
40 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# dbplyr
# note: d "b" plyr instead of dplyr
# dbplyr is the database backend for dplyr.
# It allows you to use remote database tables as if
# they are in-memory data frames by
# automatically converting dplyr code into SQL.
# install.packages("tidyverse")
library(tidyverse)
# Alternatively, install just dbplyr:
# install.packages("dbplyr")
# library(dplyr)
# library(dbplyr) isn't needed.
# dplyr autoloads dbplyr when using sql tables
# first, load in sample SQLite table
library(RSQLite)
sqlcon <- dbConnect(RSQLite::SQLite(), dbname="Chinook_Sqlite.sqlite")
# check the SQL connection by displaying the "Invoice" table
tbl(sqlcon, "Invoice")
# create an R query
sqlValues <- tbl(sqlcon, "Invoice") %>%
filter(Total > 10) %>%
collect() # collect shows the result
# what does the SQL look like?
sqlValues <- tbl(sqlcon, "Invoice") %>%
filter(Total > 10) %>%
show_query()
# Some R values or functions may not translate to SQL
# Scroll through the following for examples
# https://dbplyr.tidyverse.org/articles/translation-function.html