-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path8_readingMySQL.R
More file actions
40 lines (30 loc) · 1.45 KB
/
8_readingMySQL.R
File metadata and controls
40 lines (30 loc) · 1.45 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
## Reading from MySQL
# www.mysql.com
# Best to practice on local version of MySQL database
library(RMySQL)
## Also see package sqlfd
library(sqldf)
## Open connection to online SQL database
ucscDb <- dbConnect(MySQL(), user="genome",
host="genome-mysql.cse.ucsc.edu")
# open query to database through dbGetQuery an then disconnect!
result <- dbGetQuery(ucscDb, "show databases;"); dbDisconnect(ucscDb); # dbGetQuery has SQL arguments
result
## focus on "hg19" genome build
hg19 <- dbConnect(MySQL(), user="genome", db="hg19",
host="genome-mysql.cse.ucsc.edu")
allTables <- dbListTables(hg19)
length(allTables) # each table is a data.table
allTables[1:5]
dbListFields(hg19, "affyU133Plus2") # what are all the column fields in "affyU133Plus2"?
dbGetQuery(hg19, "select count(*) from affyU133Plus2") # how many different rows are in "affyU133Plus2"? Use a special MySQL command in ""
## Pulling data from database
affyData <- dbReadTable(hg19, "affyU133Plus2") #extract the data as a data.table
head(affyData)
## Subsetting
query <- dbSendQuery(hg19, "select * from affyU133Plus2 where misMatches between 1 and 3") # subset the data where values in mismatch column are between 1 and 3
affyMis <- fetch(query); quantile(affyMis$misMatches)
affyMisSmall <- fetch(query, n=10); dbClearResult(query); # must clear the query from the remote server when done
dim(affyMisSmall)
## Remember to close the connection!!
dbDisconnect(hg19)