Query a MySQL Database from R using RMySQL
[This article was first published on Getting Genetics Done, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I use this all the time, and the setup is dead simple. Follow the code below to load the RMySQL package, connect to a database (here the UCSC genome browser’s public MySQL instance), set up a function to make querying easier, and query the database to return results as a data frame.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Install the package if you've never done so | |
install.packages("RMySQL") | |
#Load the package | |
library(RMySQL) | |
# Set up a connection to your database management system. | |
# I'm using the public MySQL server for the UCSC genome browser (no password) | |
mychannel <- dbConnect(MySQL(), user="genome", host="genome-mysql.cse.ucsc.edu") | |
# Function to make it easier to query | |
query <- function(...) dbGetQuery(mychannel, ...) | |
# Get the UCSC gene name, start and end sites for the first 10 genes on Chromosome 12 | |
query("SELECT name, chrom, txStart, txEnd FROM mm9.knownGene WHERE chrom='chr12' LIMIT 10;") | |
# Results are returned as a data.frame: | |
# name chrom txStart txEnd | |
# 1 uc007mwj.2 chr12 3235525 3250374 | |
# 2 uc007mwg.2 chr12 3235790 3239112 | |
# 3 uc007mwh.2 chr12 3235790 3239288 | |
# 4 uc007mwi.2 chr12 3235790 3250374 | |
# 5 uc007mwk.1 chr12 3236610 3249997 | |
# 6 uc011yjq.1 chr12 3237284 3241410 | |
# 7 uc007mwl.2 chr12 3247427 3309969 | |
# 8 uc007mwm.1 chr12 3365131 3406494 | |
# 9 uc007mwn.1 chr12 3365131 3406494 | |
# 10 uc007mwp.2 chr12 3403882 3426747 |
To leave a comment for the author, please follow the link and comment on their blog: Getting Genetics Done.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.