R help

R help...


  You are here:   Home computer stuff computer help R help


I started learning R a while ago, and so have encountered problems with it along the way, mainly due to a mixture of a) me not really having a programming brain, and b) R being a tempramental entity. Sometimes I think it has a mind of its own and is trying to annoy me deliberately. However, use it I must, and below is a pool of information and advice for any novice user like me. Again, thanks to Dan Bailey, who somehow seems to find new R intricacies to play with. Also, email me to correct me if anything here is wrong.

basic R data input/output R packages plotting/graphics R scripts using seeds more info



Basic R [top]

If you are in Unix/Linux, R uses directory-specific workspaces (unlike S-Plus, for example, which will load the same global workspace, no matter from where you start it).

Here are some basic R commands to get you started.


R I/O [top]

dump("variable",file="variable.R") dumps a given variable out into a file (to be read in by source or scan)
load(<R file>) loads an R object saved with save
save("variable", file="variable.R") saves an R object (to be loaded with load). This is supposed to be "safer" than eg dump.
scan("file.R") reads an external input file into a variable (eg double data from Matlab)
sink() writes R console text (to eg a file)
source("file.R") load functions/R code into the workspace



R Packages [top]

I have co-written a semi-successful help page for creating and installing R packages.

Many thanks to Christian Stamm who has summarized and translated our guide for the German speaker.

If you are Italian, then there is a guide by Fabio Frascati on CRAN.


Plotting/graphics [top]

Devices
dev.copy(device=X11) makes an exact (X11) copy of the current device
dev.copy2eps(file="copy.eps") copies the current device to an external file (copy.eps)
dev.cur() contains value of the current device
dev.list() lists all open devices
dev.off(a) closes device number a
dev.set(a) changes current (working) device to device number a
graphics.off() closes all open (graphics) devices
get(getOption("device"))() opens a graphics device (not platform-specific)
motif() open a graphics device
X11() open a graphics device
postscript("file.ps",horizontal=FALSE,width=8, height=6.25)




creates an "external" postscript file of specified width and height (cm) [listed dimensions are "good" for journal figures]. Device should be closed with dev.off.

The commands png and pdf *should* work as well for graphics creation.


Plotting
legend(x,y,legend,fill=v) adds legend to plot with top left corner at (x,y) according to axes with key vector legend and colour description v
par(cex=0.5) changes size of graphical text/symbols (globally)
par(mfg=c(a,b,c,d)) sets current device to position (a,b) in a (c,d) multiple figure environment
par(mfcol=c(a,b)) opens an empty multiple figure environment (axb,filled column by column)
par(mfrow=c(a,b)) opens an empty multiple figure environment (axb,filled row by row)
par(xpd=NA) enables writing in plot margins
plot(...,cex=0.5) changes size of graphical text/symbols (for current plot)


For more detailed information about graphics and plotting techniques in R, there is a very good book, cunningly titled R Graphics by Paul Murrell.


R scripts [top]

A script is a useful way in Linux of running e.g. R simulations in the background. The form of the executable is like any other shell script. Here are some script argument calls for R:

R --save* saves workspace at end of a script
--no-save* doesn't save workspace at end of script
--restore restores previously stored workspace
--no-restoredoesn't restore previously stored workspace
--q no introductory information (version etc)


* to be able to dump data out from a workspace into a directory from an R script (e.g. using dump), you need to have either --save or --no-save (see the example below).

The input file to R (named inscone in the shell script example) in a script is just a list of R commands. Here is an example:

date()
library(roots)
x<-abs(runif(10))
ans<-fifthroot(x)
dump("ans",file="ans.txt")
date()
q("yes") &larr If you have a q("yes") or q("no") included at the end of your R script, this will override the specification of --save or --no-save
If you are running the script using Condor, then it seems that you also need the line source("/home/maman/.Rprofile") at the start of a script to be able to use locally installed R packages.


Using seeds [top]

R is strange, and sometimes produces the same "random" numbers if asked, for example when using rnorm or runif. To avoid pesky duplicate sets of random numbers, you could do something similar to the following (for example, for use in R scripts and simulations):

s<-system("date '+%s'",TRUE) &larr sets the seed according to a numeric interpretation of the current date/time (in seconds). Although this is strictly a Linux command, it should also work in R for windows as well. The TRUE argument makes s usable by R.
s<-as.integer(s)
set.seed(s)



More info... [top]

More information about R can be found on CRAN and the R help mailing lists.