Difference between revisions of "R"

From ArchWiki
Jump to: navigation, search
m (Variables)
(Configuration: editing the Renviron.r file)
Line 64: Line 64:
 
  > update.packages(ask=FALSE,checkBuilt=TRUE)
 
  > update.packages(ask=FALSE,checkBuilt=TRUE)
  
==Configuration==
+
==Configuration files==
 +
 
 +
The two user configuration files you want in your home folder are {{ic|.Rprofile}} and {{ic|Renviron.r}}. If you want to keep your {{ic|$HOME}} directory as clean as possible, a good practice will be to make the {{ic|~/.config/r}} directory and ''append'' all your environment [[#Variables]]  in the {{ic|Renviron.r}} file : {{ic|$HOME/.config/r/Renviron.r}}. Then, at the root of your {{ic|$HOME}} directory, append the following line to {{ic|~/.Rprofile}}:
 +
  source ("~/.config/r/Renviron.r")
 +
===Basic Renviron.r===
 +
Here is a very basic {{ic|Renviron.r}} you can start with:
 +
{{hc|Renviron.r|                                     
 +
Sys.setenv(R_HOME_USER = "/path/to/your/r/directory")
 +
Sys.setenv(R_ENVIRON_USER = "${HOME}/.config/r/.Renviron.r")
 +
Sys.setenv(R_LIBS_USER = "/path/to/your/r/library")
 +
Sys.setenv(R_HISTFILE = "/path/to/your/filename.Rhistory")  # Do not forget to append the '''.Rhistory'''
 +
Sys.setenv(R_HISTSIZE ='5000')
 +
 
 +
readRenviron("/path/to/any/custom_startup_file")            # read any custom startup file
 +
}}
 +
 
 +
You can add more [http://stat.ethz.ch/R-manual/R-devel/library/base/html/options.html|global options] to customize your {{ic|R}} environment. Here is a very short list:
 +
{{hc|Renviron.r|
 +
options(prompt = paste(paste (Sys.info () [c ("user", "nodename")], collapse = "@")," R > "))  # customize your R prompt with username and hostname this way: '''user@hostname R >'''
 +
options(digits = 4)                                                                                # number of digits to print
 +
options(stringsAsFactors = FALSE)
 +
options(show.signif.stars = FALSE)
 +
error = quote(dump.frames("${R_HOME_USER}/testdump", TRUE))
 +
}}
  
 
== Adding a graphical frontend to R ==
 
== Adding a graphical frontend to R ==

Revision as of 20:30, 25 October 2013

Tango-document-new.pngThis article is a stub.Tango-document-new.png

Notes: please use the first argument of the template to provide more detailed indications. (Discuss in Talk:R#)

R is a free software environment for statistical computing and graphics (http://www.r-project.org/).

Installation

Install the r package available in the official repositories

Some external packages may require to be compile in Fortran as well, so installing the gcc-fortran can be a good idea

Initial configuration

Please refer to Initialization at Start of an R Session to get a detailed understanding of startup process. The home directory of the R installation is usr/bin/R. Base packages are found in usr/bin/R/library/base and site configuration files in /etc/R/. Aspects of the Locale are accessed by the functions Sys.getlocale and Sys.localeconv within the R session. Locales will be the one defined in your system.

To start a R session, open your terminal and type this command:

$ R
Note:
  • Use Shift+u for the command (some terminals use the r letter to repeat the last entered command). Once in your R session, the prompt will change to >
  • site refers to system-wide in R Documentation

Run help(startup) to read the documentation about system file configuration, help() for the on-line help,help.start() for the HTML browser interface to help, demo() for some demos and q() to close the session and quit.

When closing the session, you will be prompted : Save workspace Image ?[y/n/c]. The workspace is your current working environment and include any user-defined objects. The saved image is stored in .RData and will be automatically reloaded the next time R is started. You can manually save the workspace at any time in the session with the save.image() command.

Variables

R can be confusing when it comes to Environment Variables, as they are large and duplicated following the site or user sides. Most important variables can be found on Environment Variables R Documentation. At startup, R search at early stage for site and user Renviron files to process for setting environment variables. The name of the site file is the one pointed to by the environment variable R_ENVIRON, given when running the R.home() command within R session.
> R.home()
[1] "/usr/lib64/R"
Note: /usr/lib64/R/etc/Renviron is a symlink to /etc/R/Renviron

The name of the user file can be specified by the R_ENVIRON_USER environment variable. If you don't specify any file, R will automatically read .Renviron in your home directory if there is one. As this file is read at start-up early stage, you can write a lot of initialization informations in it using the R language syntax. The style you will use to declare your variables will follow the name=value scheme:

 Sys.setenv(VARIABLE_NAME="path/to/directory")

Lines can be either commented starting with # or in the above mentioned style.

Note: if you want to use system environment variable, you need to put the name of the variable in braces. E.g : $HOME will be ${HOME}

Most useful Environment Variables you want to use are the following ones:

R_HOME_USER
directory where you want to put all all your R stuff like custom functions, .RData workspace, projects
R_LIB_USER
directory where you will install your packages
R_HISTFILE
path where you will store your command history file
R_HISTSIZE
maximum lines
R_PDFVIEWER
path to defaut pdf viewer (in X11, usually /usr/bin/xdg-open)
PAGER
path to defaut pager
EDITOR
path to defaut editor

To see a list of your current variables, run options() within your R session

Installing R packages

There are many add-on R packages, which can be browsed on The R Website.. They can be installed from within R using the install.packages(pkgname) command. R can install its packages locally as per user local settings or system wide.

Within your R session, run this command to check that your user library exists and is set correctly:

> Sys.getenv("R_LIBS_USER")
[1] "/path/to/directory/R/packages"

Installation within your R session is the safest way and won't conflict with the pacman package management, but there is another method to install packages. Run the following command in your terminal:

$ R CMD INSTALL -l $R_LIBS_USER pkg1 pkg2 ...

Upgrading R packages

> update.packages(ask=FALSE)

Or when you also need to rebuild packages which were built for an older version:

> update.packages(ask=FALSE,checkBuilt=TRUE)

Configuration files

The two user configuration files you want in your home folder are .Rprofile and Renviron.r. If you want to keep your $HOME directory as clean as possible, a good practice will be to make the ~/.config/r directory and append all your environment #Variables in the Renviron.r file : $HOME/.config/r/Renviron.r. Then, at the root of your $HOME directory, append the following line to ~/.Rprofile:

 source ("~/.config/r/Renviron.r")

Basic Renviron.r

Here is a very basic Renviron.r you can start with:

Renviron.r
                                       
Sys.setenv(R_HOME_USER = "/path/to/your/r/directory")
Sys.setenv(R_ENVIRON_USER = "${HOME}/.config/r/.Renviron.r")
Sys.setenv(R_LIBS_USER = "/path/to/your/r/library")
Sys.setenv(R_HISTFILE = "/path/to/your/filename.Rhistory")   # Do not forget to append the .Rhistory
Sys.setenv(R_HISTSIZE ='5000')

readRenviron("/path/to/any/custom_startup_file")             # read any custom startup file

You can add more options to customize your R environment. Here is a very short list:

Renviron.r
options(prompt = paste(paste (Sys.info () [c ("user", "nodename")], collapse = "@")," R > "))  # customize your R prompt with username and hostname this way: user@hostname R >
options(digits = 4)                                                                                # number of digits to print
options(stringsAsFactors = FALSE)
options(show.signif.stars = FALSE)
error = quote(dump.frames("${R_HOME_USER}/testdump", TRUE))

Adding a graphical frontend to R

The linux version of R does not include a graphical user interface. However, third-party user interfaces for R are available, such as R commander and RKWard.

R Commander frontend

R Commander is a popular user interface to R. There is no Arch linux package available to install R commander, but it is an R package so it can be installed easily from within R. R Commander requires Tk:

# pacman -S tk

To install R Commander, run 'R' from the command line. Then type:

> install.packages("Rcmdr", dependencies=TRUE)

This can take some time.

You can then start R Commander from within R using the library command:

> library("Rcmdr")

RKWard frontend

RKWard is an open-source frontend which allows for data import and browsing as well as running common statistical tests and plots. You can install rkwardAUR from AUR.

Rstudio IDE

RStudio an open-source R IDE. It includes many modern conveniences such as parentheses matching, tab-completion, tool-tip help popups, and a spreadsheet-like data viewer.

Install rstudio-desktop-binAUR (binary version from the Rstudio project website) or rstudio-desktop-gitAUR (development version) from AUR.

The R library path is often configured with the R_LIBS environment variable. RStudio ignores this, so the user must set R_LIBS_USER in ~/.Renviron, as documented above.