forked from DarrellAucoin/IntroSQL
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUsing_SQL_with_R_Presentation.Rmd
More file actions
209 lines (149 loc) · 3.82 KB
/
Using_SQL_with_R_Presentation.Rmd
File metadata and controls
209 lines (149 loc) · 3.82 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
---
title: "Using SQL with R"
author: "Darrell Aucoin (Stats Club)"
date: "February 24, 2015"
output: ioslides_presentation
---
## Required Packages
For SQL within R, we usually need two packages:
1. __DBI__: R Database Interface
2. Specific package for the individual SQL implementation (MySQL, SQLite, Oracle, etc.).
- Package `RSQLite` for SQLite
## DBI Functions: Driver Specification
DBI contains various virtual classes and functions in connecting and querying a database:
__dbDriver__ Driver specifying the operations for creating connections to SQL Servers
For SQLite:
```r
m = dbDriver("SQLite") # equivalent to SQLite()
```
For MySQL:
```r
m = dbDriver("MySQL") # equivalent to MySQL()
```
## DBI Functions: Connecting to DBMS
__dbConnect__ Connect to a DBMS.
For SQLite:
```r
conn = dbConnect(m, "path_to_file")
```
For MySQL:
```r
conn = dbConnect(m, user="user_name", db="database_name",
host="SQL_server", password="password")
```
__dbDisconnect__ Disconnect from a DBMS. You should always disconnect after you no longer need it
```r
dbDisconnect(conn)
```
## DBI Functions: Submit Queries
__dbSendQuery__ Submits and executes SQL statement (information retrieved using fetch)
```r
query = dbSendQuery(conn, "SELECT statement;")
```
__dbColumnInfo__ Describes the meta-data of a query (data types ect.)
```r
dbColumnInfo(query)
```
__fetch__ Get records from a dbSendQuery
```r
res = fetch(query)
```
__dbClearResult__ _Free all resources_ (local and remote) from a query.
```r
dbClearResult(query)
```
## DBI Functions: Other Functions
__dbGetQuery__ Submits, executes SQL statement and retrieves information
```r
res = dbGetQuery(conn, "SELECT statement;")
```
__dbListTables__ List tables in database connection
```r
tables = dbListTables(conn)
```
__dbListFields__ Return the column names for a given table
```r
columns = dbListFields(conn, "table_name")
```
__dbGetInfo__ Get meta-data for DBIObjects
```r
meta.data = dbGetInfo(query)
```
---
__dbReadTable__ Fetch the data from a table.
```r
res = dbReadTable(conn, "table_name")
```
__dbWriteTable__ Write a dataframe to SQL database.
```r
dbWriteTable(conn, "DBMS_table_name", data.frame.name)
```
## Connecting a SQLite Database
```{r}
library(RSQLite)
m = dbDriver("SQLite")
con = dbConnect(m, "~/GitHub/IntroSQL/stats_club.db")
query = dbSendQuery(con, "SELECT name, position FROM exec")
result = fetch(query)
dbClearResult(query)
dbDisconnect(con)
```
---
```{r}
result
```
---
```{r}
library(RSQLite)
m = dbDriver("SQLite")
con = dbConnect(m, "~/GitHub/IntroSQL/stats_club.db")
event_table = dbGetQuery(con,
"SELECT name, type, start_time, location
FROM event")
event_table
dbDisconnect(con)
```
## Connecting to Local File System using MySQL
I transferred the data in stats_club.db to MySQL on my laptop.
```{r}
library("RMySQL")
m = dbDriver("MySQL")
conn = dbConnect(m, user="darrell", db="stats_club",
host="localhost",
password="pass")
```
---
```{r}
query = dbSendQuery(conn, "SELECT name, type, start_time, location
FROM event")
result = fetch(query)
dbClearResult(query)
dbDisconnect(conn)
```
---
```{r}
print(result)
```
## Connecting to Remote Server using MySQL
```{r}
hg19 = dbConnect(MySQL(), user="genome", db="hg19",
host="genome-mysql.cse.ucsc.edu")
result = dbGetQuery(hg19,
"SELECT COUNT(*) FROM affyU133Plus2")
dbDisconnect(hg19)
print(result)
```
## Using SQL Statements with Native R Data-Frames
It is even possible to do SQL-like queries on native R Data-Frames.
- We can do some quick and dirty data manipulation without using the `dplyr` package
- Note that we still have the R dataframe event_table
```{r}
event_table
```
---
```{r}
library(sqldf)
sqldf('SELECT type, COUNT(*)
FROM event_table
GROUP BY type')
```