R

From UC_Chemistry

Jump to: navigation, search

Contents

Install R

  • Download file from here and move it into your home directory. Then
./configure
make

if you have root priviledge

make install

otherwise you don't have to install R; run it from the R-XXX/bin/ directory or create symbolic link in any place

ln -s /home/user-name/R-XXX/bin/R R

the link will be place in the current folder.

Without root priviledge you also have to export R_LIBS variable

export R_LIBS=/home/user-name/R-2.5.0/library

i think you have to write this line in ".bash_profile" file in your home directory

Extra Packages

  • To install a package, type in any place (provided you already can run R)
R CMD INSTALL package-name.tar.gz
  • Most of the packages are at CRAN
    • Bio3d is a must-have package for computational chemist
    • Ade4 labels on 2D scatter plot
    • R commander a GUI for windows (i think)
    • XML xml parser

Links

  1. R and BioConductor
  2. StatsRUs
  3. R-project
  4. Short Reference Card
  5. R-tutorial
  6. Wiki
  7. Code Optimization

R basic

  • Load and save R objects
a <- c(1,2,3)
save(a, file="test.RData")
rm(a)
load("test.RData")
  • Load data and plot XY
myplot <- read.table("file_with_two_columns.xy",header=T,sep=" ")
plot(myplot)
  • Save plot to in ps-format
postscript("myfile.ps")
plot(myplot)
dev.off()

if you want to convert to epsi format, in command line type: system("ps2epsi myfile.ps")

  • This example is marvelous, it will create 5 random normal distributions and will save in files fig1.jpg, fig2.jpg ..., fig5.jpg
for(i in 1:5) {
jpeg(paste("fig", i, ".jpg", sep = ""))
hist(rnorm(100))
dev.off()
}

Then in command line create animation (look Animated_Gifs for details)

convert -delay 50 -loop 50 fig*.jpg animated.gif
  • Create 2 x 5 matrix with numbers from 1 to 10
x <- matrix(1:10,ncol=5)

Scripts

  • All R commands can be saved in a script, say foo.R and then executed as follows
R CMD BATCH foo.R &

R packages

Using R packages

Currently loaded packages

search()

See installed packages and load a package

library()
library(packagename)

HTML documentation of all installed packages

help.start()

Making R packages

Reference notes

  • devices

To see all active devices

dev.list()

To change device

X11()
postscript("file.eps", horizontal=FALSE, height=8, width=6, pointsize=10, onefile=FALSE)   # file.ps could be used as well
pdf()
png()
jpeg()

Don't forget to terminate the device driver when done

dev.off()
  • plot
plot(object, main="title", 
     xlab="x-label", xlim=c(xmin,xmax),          # same applies for y
     type="l",                                   # l for lines, p for points, b for both 
     lab=c(nx_tics, ny_tics, xlabel_length), 
     log="xy"                                    #indicates with axis to put on log scale
)
  • put text on plot
xpos <- 10
ypos <- 10
text(xpos, ypos,expression(paste(the text in latex-like format goes here)))
  • identify (identify points on your plot by clicking the button on mouse, right button returns a list of all points identified)
identify(object)
Personal tools