Advanced usage of R with RMySQL

R called inside other applications

One thing that occasionally bothers me is how to refer a variable inside a function call, now it becomes more prominent when I try to use a parameter passed into a function from a RMySQL query. Here is the deal:

An example in RMySQL from RBlog

#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;")

My own database

mirList <- c("Adrenal", "Brainstem")
numOfTis <- length(mirList)	
query <- function(...) dbGetQuery(mydb, ...)
query("select rnMiR as mir, tissue as tis from TisSpeMiRs where tissue in (\"Adrenal\", \"Brainstem\") and ctrCode = 2 limit 2 ;") ## this works
query("select rnMiR as mir, tissue as tis from TisSpeMiRs where tissue in eval(mirList) and ctrCode = 2 limit 2 ;") ## this does NOT work!!

Well, some “expert” posted Advanced R usage
Single quote vs. double quote is here

Learning step 1

	
  • Option 1
  • min.v<-5 max.v<-10 cat("You entered ", "\"", min.v, " ", max.v,"\"", sep="")
  • Option 2
  • cat("You entered ", '"', min.v, " ", max.v,'"', sep="")
  • Option 3
  • options(useFancyQuotes=FALSE) cat("You entered ", dQuote(paste(min.v, max.v)), sep="")
  • Option 4
  • options(useFancyQuotes=FALSE) cat("You entered (", dQuote(paste(min.v, max.v)), ")", sep="") cat("You entered (", paste(dQuote(min.v), dQuote(max.v), sep = ",") , ")", sep="") num <- c(min.v, max.v) cat("You entered (", dQuote(paste(num)), ")", sep="") cat("You entered (", paste(num, collapse = ",") , ")", sep="") cat("You entered (", paste(dQuote(paste(num)), collapse=","), ")", sep="")

    Learning step 2

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.