Created
June 14, 2010 19:54
-
-
Save keithshep/438210 to your computer and use it in GitHub Desktop.
Cheat-sheet for R
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
# attach and detach a package: | |
library("MouseDivGeno") | |
detach(package:MouseDivGeno) | |
# start in 64 bit mode: | |
R --arch x86_64 | |
# Stack-trace in R: | |
traceback() | |
# Break on error in R: | |
options(error = recover) | |
# Turn warnings into errors | |
options(warn = 2) | |
# Acts as a code break-point so you can inspect vars | |
browser() | |
# trim off the 1st and last chars of a string: | |
substr("hello", start=2, stop=nchar("hello") - 1) | |
# the apply most similar to haskell's (map f [1..20]) | |
mapply(f, 1:20) | |
# if you would rather have a vector than a list you can do | |
sapply(f, 1:20) | |
# Here is the R version of haskell's zipWith | |
mapply(get("+"), as.list(1 : 10), as.list(31: 40), SIMPLIFY = F) | |
# apply a function to matrix margins first by row then by column | |
apply(matrix(1:12, nrow = 3), 1, sum) | |
apply(matrix(1:12, nrow = 3), 2, sum) | |
# find difference between neighboring values | |
diff(1:10) # returns nine 1s | |
# Run Length Encoding | |
rle(...) | |
inverse.rle(...) | |
# replace file extension with empty string | |
sub("\\..*$", "", "helloworld.exe") | |
# find string manipulation functions | |
help.search(keyword="character") | |
# Find all installed packages matching a substring | |
grep("MouseDivGeno",rownames(installed.packages()), value = T) | |
# file stuff | |
myPath <- file.path(baseDir, "tmp.txt") | |
if(file.exists(myPath)) | |
{ | |
con <- file(description = myPath, open = "rt") # open can be: rt, wt, rb, rb, at ... | |
... do something ... | |
close(con) | |
} | |
dir.create("some-dir-name") | |
# Setting and getting using character names | |
assign("varname", matrix(1 : 10)) | |
get("varname") | |
# setting terminal width | |
options(width=Sys.getenv("COLUMNS")) | |
# shuffle a vector | |
sample(1 : 30) | |
# machine specific info like maximum integer | |
.Machine$integer.max | |
# For using optional packages: | |
# if package is missing use _R_CHECK_FORCE_SUGGESTS_=false | |
# In code you can do something like | |
# | |
# pkg <- "somePkg" | |
# pkgAvail <- require(pkg, character.only = TRUE) | |
# if (pkgAvail) | |
# ... | |
# else | |
# ... | |
# to quickly test out what a color looks like: | |
image(matrix(1), col= "grey") | |
Creating binary packages on windows: | |
==================================== | |
* Install latest version of R (choosing non-default install location is OK) | |
* Install latest version of Rtools (refer to http://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset) | |
* make sure that the R and Rtools bin dirs are on your %PATH% | |
* If you have your R code in a tarball or zip file you need to unzip it. | |
* Enter: "Rcmd check <package_src_dir>" to check on any package errors or warnings | |
* Enter: "Rcmd build --binary <package_src_dir>" on the command line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment