eval_db <- FALSE if(Sys.getenv("GLOBAL_EVAL") != "") eval_db <- Sys.getenv("GLOBAL_EVAL")
library(connections) library(RSQLite) library(dplyr) library(dbplyr)
dplyr
connections
Use connections
to open open a database connection
Load the connections
package
r
library(connections)
Use connection_open()
to open a Database connection
r
{{db_connection}}
The RStudio Connections pane should show the tables in the database
Use the dplyr
's tbl()
command
Load the dplyr
package
r
library(dplyr)
Use the tbl()
command to call the customer table
r
tbl(con, "customer")
Add in_schema()
as an argument to tbl()
to specify the schema. The default schema is called: "main"
r
tbl(con, in_schema("main", "customer"))
Load the results from the tbl()
command that points to the table called order to a variable called order
r
order <- tbl(con, "order")
Use show_query()
to preview the SQL statement that will be sent to the database
Use show_query()
to preview SQL statement that actually runs when we run order
as a command
r
show_query(order)
Easily view the resulting query by adding show_query()
in another piped command
r
order %>%
show_query()
Insert head()
in between the two statements to see how the SQL changes
r
order %>%
head() %>%
show_query()
Use sql_render()
and simulate_mssql()
to see how the SQL statement changes from vendor to vendor
r
order %>%
head() %>%
sql_render(con = simulate_mssql())
Use explain()
to explore the query plan
r
order %>%
head() %>%
explain()
Review of how dbplyr
handles R commands that have not been translated into a like-SQL command
Preview how Sys.time()
is translated
r
order %>%
mutate(today = Sys.time()) %>%
show_query()
Use SQLite's native commands, in this case date()
r
order %>%
mutate(today = date('now')) %>%
show_query()
Run the dplyr
code to confirm it works
r
order %>%
mutate(today = date('now')) %>%
select(today) %>%
head()
Intro on passing unevaluated code to a dplyr verb
Preview how Sys.time()
is translated when prefixing !!
r
order %>%
mutate(today = !!Sys.time()) %>%
show_query()
Preview how Sys.time()
is translated when prefixing !!
r
order %>%
mutate(today = !!Sys.time()) %>%
select(today) %>%
head()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.