Mines-Intro-To-R
Table of Contents
1 R configuration
The Mines Paristech unix machines have access to R-2.7.1 at /usr/bin/R. This is an old version that requires custom configuration of the user library in order to use third party packages. If you are using a newer version of R, you can skip the following section.
1.1 Configure R user library and install packages at Mines ParisTech
In order to use R packages from CRAN, you need to set up a personal library in ~/R/library, and tell R to use a proxy to access the web: http://www-proxy.ensmp.fr:8080
This should be no problem if you copy and paste the following R code into your ~/.Rprofile
ldir <- "~/R/library" if(!file.exists(ldir)){ dir.create(ldir,recursive=TRUE) } .libPaths(c(ldir,.libPaths())) options(browser="firefox") options(repos=c("http://cran.univ-lyon1.fr/", "http://cran.r-project.org")) Sys.setenv(http_proxy="http://www-proxy.ensmp.fr:8080")
1.2 Configuration for newer versions of R
If you installed R on your personal computer then you probably don't have a proxy, and you probably have a newer version of R that does the user library configuration automatically.
1.3 Install packages
Then open a terminal, type R, and copy/paste the following lines of code to install the required packages
install.packages("ROCR") source("http://bioconductor.org/biocLite.R") biocLite("ALL")
2 Basic R usage
2.1 The R interpreter and language constructs
The basic idea is that R is a command line interpreter. Write code in a text editor like medit, which is installed by default on the Mines computers and has basic support for syntax highlighting of R code. When you want to execute the R code, you can either copy/paste code into R running in a terminal, or in R do
source("my.script.R")
which will execute all the code in the file my.script.R
To learn the basics of R you should print out a copy of http://cran.r-project.org/doc/contrib/Short-refcard.pdf
- <- and = are both assignment operators
- f(a,b,c) calls function f with arguments a,b,c. do.call(f,list(a,b,c)) does the same thing.
- for(x in L){commands} loops over items in L
- install.packages("kernlab") downloads and installs the kernlab package and all its dependencies from the Comprehensive R Archive Network (CRAN).
- library(kernlab) loads the kernlab package and all its dependencies, which should be already installed on your computer.
- plot(x,y) creates a scatterplot based on the vectors x and y, and par() controls graphical parameters.
- table(x,y) shows a contingency table of all unique values in the vectors x and y.
2.2 Getting help
R contains an extensive help system. To illustrate, say we want to find a function that gives us the Student t-distribution. First type help.search("student") and you will see a list with relevant help pages. The one you want is TDist. Type ?TDist and you will see a help page that explains:
- the density is dt()
- the CDF is pt()
- the inverse CDF (quantile function) is qt()
- random numbers can be generated with rt()
To get help on a function, type ? and then the function name. For example, ?plot shows you the help page for the plot function. Or type help.start() to get a HTML help interface.
2.3 Vectorized language
R is a vectorized language. That is, when you type x*y it means elementwise multiplication of the vectors x and y.
## DONT DO THIS: z <- x for(i in 1:length(x)){ z[i] <- x[i] * y[i] } ## YES DO THIS: (does the same but much more efficient) z <- x * y
2.4 Tab completion and keyboard shortcuts
At the R command line, you can use TAB completion for variables, functions, and arguments. For example if you forgot the name for the function that can read a data table, you can just type "read", then TAB twice will give you a list of possible functions. After typing "read.t" TAB will fill in and give you "read.table". Add a parenthesis to make it "read.table(" and hitting TAB twice will show you a list of valid keyword arguments to the read.table function.
Some useful keyboard shortcuts. C-a means hold control and type a. M-f means hold Alt and type f.
shortcut | function |
---|---|
C-p or up arrow | recall previous command |
C-n or down arrow | recall next command |
C-a | go to beginning of line |
C-e | go to end of line |
M-f | go forward 1 word |
M-b | go backward 1 word |
M-d | delete word in front |
C-delete | delete word in back |
C-k | delete until end of line |
C-y | yank (paste) deleted word(s) |
3 Advanced R usage with Emacs and ESS
Emacs Speaks Statistics (ESS) provides a better development environment for R programming. http://ess.r-project.org/index.php?Section=download
The idea is that instead of running R in a terminal, and editing R code in a separate window, you do both from within Emacs. This streamlines the programming process, making writing and testing R scripts much faster.
3.1 Installing on Mines ParisTech machines
To use it on the Mines Paristech unix machines, execute the following code in a terminal to download and setup ESS:
cd mkdir R cd R wget http://ess.r-project.org/downloads/ess/ess-5.13.tgz tar xf ess-5.13.tgz cat >> ~/.emacs <<EOF (add-to-list 'load-path "~/R/ess-5.13/lisp") (load "ess-site") (setq ess-eval-visibly-p nil) (setq ess-ask-for-ess-directory nil) (require 'ess-eldoc) (show-paren-mode 1) EOF wget http://cbio.ensmp.fr/~jvert/svn/tutorials/practical/svmbasic/svmbasic.R wget http://cbio.ensmp.fr/~jvert/svn/tutorials/practical/svmbasic/general.R
This code should also work on any other Linux system with Emacs and R.
3.2 Installing on windows or mac
Vincent Goulet maintains excellent Emacs distributions that work out-of-the box with R
3.3 Interacting with R from Emacs
Now you can do open up an R code file in Emacs from the command line using, for example "emacs svmbasic.R &"
Alternatively, from within Emacs you can start R by typing M-x R ENTER.
When writing R code from within Emacs you get context-sensitive help. For example, after typing "read.table(" Emacs will display the list of arguments for the read.table function.
Additionally, you can use the following keyboard commands in a R code file to evaluate R code:
Shortcut | Function |
---|---|
C-c C-n | Send 1 line of R code |
C-c SPACE | Mark current position |
C-c C-r | Send code between mark and point |
C-c C-l | Send entire buffer |
C-c TAB or C-c C-i | Complete |
Furthermore the R from within Emacs buffer has similar shortcuts to R in the terminal, but with the following exceptions:
Shortcut | Function |
---|---|
M-p | Recall previous command |
M-n | Recall next command |
C-p | Move cursor up |
C-n | Move cursor down |
3.4 Advanced Emacs techniques
Further information about Emacs is beyond the scope of this guide, but I suggest you take the Emacs tutorial by typing C-h t from within Emacs. Or download the French version and open it in Emacs
Org version 7.5 with Emacs version 22
Validate XHTML 1.0