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

    MongoDB with Java and Python

    In order to work on project MongoDB with Java, there are a few setups

    • Install MongoDB

     

    • Make sure Maven is installed

     

    It turns out that I stick with mongo + python so I can leverage the opportunity to sharpen my python skills. 

    From here on, I will keep accumulating mongo/python related cases

    I have a mongodb dump, and I can restore it.

    	
  • To create a dump:
  • Go the directory where the "dump" exists
  • Issue command "mongorestore dump"
  • It shall restore the databse
  • I have a grade information in .json format

    cat grades.json | python -m json.tool | more
    

    Graphical Database Schema

    In order to get approval, we need a graphical diagram. Pierre suggest schemaspy. It turns out that a couple of dependencies were required:

    	
  • Apparently, SchemaSpy
  • It needs Graphviz installed
  • Also, database driver in our case JDBC-MySQL driver
  • Luckily, I have it installed on my windows machine with ColdFusion earlier, located at
  • C:\ColdFusion2016\cfusion\lib\mysql-connector-java-5.1.38-bin.jar
  • Migrate mysql DB from linux (wine) to windows desktop. Before doing this, I needed to drop all tables in the database first. It turns out a scripting way works for linux. For windows, it is easier to drop the database and re-create the database.

    [li11@ehscmplp11/wine ~]$ mysqldump -u li11 -ppassword mirDB > ~/mirDB.sql
    C:\Users\li11>mysql -u root -ppassword ratemirs < x:\mirDB.sql
    

    My command to run SchemaSpy was:

    java -jar Downloads\schemaSpy_5.0.0.jar -dp C:\ColdFusion2016\cfusion\lib\mysql-connector-java-5.1.38-bin.jar -t mysql -db RATEmiRs -host localhost -u li11 -p nopassword -o X:\project2016\microRNADB\diagram\

    Interesting enough, the command involves both “windows” local and “network” drive.

    10/18/2016

    I did not know what was going wrong, but when I tried the same command, it failed then became okay. Just to watch out for it.