-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
138 lines (100 loc) · 2.57 KB
/
README.Rmd
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
title: "r_sql"
author: "Daniel Chen"
date: ''
output:
html_document:
keep_md: yes
toc: yes
html_notebook: default
---
```{r setup, include=FALSE}
SUPER_SECRET_PASSWORD <- ""
knitr::opts_chunk$set(echo = TRUE)
```
# Connecting to a SQL database
```{r, eval=FALSE}
library(sdalr)
library(DBI)
library(RPostgreSQL)
con <- con_db(dbname = 'arlington', user = 'chend')
```
```{r, echo=FALSE}
# DELETE PASSWORD WHEN COMMITING
library(sdalr)
library(DBI)
library(RPostgreSQL)
con <- con_db(dbname = 'arlington', user = 'chend', pass = SUPER_SECRET_PASSWORD)
```
```{r}
con
```
# Getting (Querrying) data from database in R
```{r}
df <- dbGetQuery(con, "")
head(df)
```
You can also use an `sql` chunk
```
```{sql connection=con}
SELECT * FROM fire.medic_unit_movement_summary_2013;
```
```
```{sql connection=con}
SELECT * FROM fire.medic_unit_movement_summary_2013;
```
# Creating Tables
Everyone should have a database named their pid.
```{r, eval=FALSE}
con <- con_db(dbname = 'chend', user = 'chend')
```
```{r, echo=FALSE}
con <- con_db(dbname = 'chend', user = 'chend', pass = SUPER_SECRET_PASSWORD)
```
```{r, results='hide'}
# delete a table (you have permissino to do)
dbSendQuery(con, "DROP TABLE customers;")
# create a new table
dbSendQuery(con, "CREATE TABLE customers (customer_no INTEGER, first_name TEXT, last_name TEXT);")
# add row information into a table
dbSendQuery(con, "INSERT INTO customers (customer_no, first_name, last_name) VALUES (1, 'MC', 'Hammer')")
dbSendQuery(con, "INSERT INTO customers (customer_no, first_name, last_name) VALUES (2, 'Hammer', 'Time')")
```
```{r}
dbGetQuery(con, "SELECT * from customers;")
```
```{r}
# save results to an R data frame
df <- dbGetQuery(con, "SELECT * from customers;")
df
```
# Get data in and out of chunks
When working with chunks that use different language engines, you will need a way to pass
R values into the engine, and get values back into the R environment
## Get R data into chucnks
To get an `R` variable into a chunk, you prepend the R variable by a `?`, e.g., `?x` if you want to use the variable `x` in the chunk
```{r}
x <- 'Time'
```
```
```{sql connection=con}
SELECT * FROM ?x;
```
```
```{sql connection=con}
SELECT * FROM customers WHERE last_name = ?x;
```
## Get chucnk values into R
You can get values and results back into the R enviornment by using the `output.var` chunk option
```
```{sql connection=con, output.var=df_sql}
SELECT * FROM ?x;
```
```
```{sql connection=con, output.var='df_sql'}
SELECT * FROM customers WHERE last_name = 'Hammer';
```
In R:
```{r}
df_sql
```