R notes


ggplot2

Hide ggplot legend

theme(legend.position="none")

tryCatch syntax

result <- lapply(my_iterator, function(x) tryCatch(my_function(x), error = function(e) FALSE))

R Syntax

mapply
assign
save(list=blah, file="blah.RData") # blah is a character string with an object name

attach full namespace for testing purposes

attach(getNamespace(“qtl”))

another idea

assignInNamespace(“stepwiseqtl”, stepwiseqtlQuoc, ns=”qtl”)

utf-16 codes

rightarrow: 2192

“\u2192”

(doesn’t seem to work in jpg files, but does in png)

Building R packages in Windows R CMD INSTALL –build –compile-both qtl_1.32-3.tar.gz

Avoid read.csv (due to the default fill=TRUE) Use read.table(…, header=TRUE, sep=”,”) fill=TRUE behaves terribly if later rows have extra columns


reinstalling all R packages: update.packages(checkBuilt=TRUE, ask=FALSE)


installing gfortran: (http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2) sudo tar fxz ~/Desktop/gfortran-4.8.2-darwin13.tar.bz2 -C /


in ~/.R/config:

CC=clang CXX=clang++ F77=gfortran-4.8 FC=$F77 OBJC=clang

Also, in ~/.R/Makevars

CFLAGS=-g -O2 -Wall -pedantic CXXFLAGS=-g -O2 -Wall -pedantic


Installing from github with vignettes:

install_github(“ropensci/rentrez”, build_vignettes=TRUE)

[install_github passes extra args, including build_vignettes, to install()]


rgl package:

  • had a problem that X11 wasn’t found, used:

    sudo ln -s /opt/X11 /usr/X11

  • still having a problem, with libpng. StackOverflow (http://bit.ly/1R01mV7) says:

    R CMD INSTALL --with-libpng-prefix=/usr/X11/lib/ rgl
    

    Got the same error (can’t load libpng); gave up and went with the (older) binary version of the package.


complete.cases: identifies the rows of a matrix that have no NAs


Environment variables

Sys.setenv(“NOT_CRAN”=”true”) Sys.unsetenv(“NOT_CRAN”) Sys.getenv(“NOT_CRAN”)


setNames: create and set names for an object at the same time.

setNames(1:3, c(“first”, “second”, “third”))


address of an object; useful to see if it’s been copied

See http://adv-r.had.co.nz/memory.html#modification

plyr::address(x) # address of object plyr::ref(x) # number of references to object