Installing packages:
install.packages('sp')
View version of installed packages:
packageVersion('sp')
packageDescription('sp')['Version'] # more verbose
# Print version information about R and attached or loaded packages.
sessionInfo()
Install package from tar file:
R CMD INSTALL /path/to/file
Install package from its source directory:
cd /path/to/project/source
Rscript -e "install.packages('$PWD', repos=NULL, type='source')"
Print list of functions in a package:
ls('package:achia')
Unload / detach library:
detach('package:sp', unload=T)
Installation path of R versions:
/Library/Frameworks/R.framework/Versions
Store a variable inside a package's environment:
.AchiaEnv <- new.env()
set.workdir <- function(path) {
assign('WORKDIR', path, envir=.AchiaEnv)
}
get.workdir <- function() {
get('WORKDIR', envir=.AchiaEnv)
}
Use this first line for R scripts:
#!/usr/bin/env Rscript
Ask for user input in Rscript:
cat('\n[Press Enter]\n')
foo <- scan("stdin", character(), n=1, nlines=1, quiet=TRUE)
Get help about any R function or structure:
# documentation of the `rle` function
?rle
# documentation of the `read.csv` function
?read.csv
# list objects in memory
ls()
# delete objects from memory
rm('variablename')
# delete all objects
rm(list = ls())
# current working directory
getwd()
# change to another directory
setwd('/path/to/somewhere/else')
# these are the same
subset(airquality, Month == 5)
subset(airquality, airquality[,'Month'] == 5)
subset(airquality, airquality[,5] == 5)
Modulo operator:
# 5 mod 3 is 2
# 3 mod 5 is 3
i %% n