Difference between revisions of "R"

From ArchWiki
Jump to: navigation, search
m (Initial configuration)
(Installation: Total revamp and rewrite with more details)
Line 22: Line 22:
 
When closing the session, you will be prompted : {{ic|Save workspace Image ?[y/n/c]}}. The ''workspace'' is your current working environment and include any user-defined objects. The saved image will be automatically reloaded the next time {{ic|R}} is started. You can manually save the workspace at any time in the session with the {{ic|save.image()}} command.
 
When closing the session, you will be prompted : {{ic|Save workspace Image ?[y/n/c]}}. The ''workspace'' is your current working environment and include any user-defined objects. The saved image will be automatically reloaded the next time {{ic|R}} is started. You can manually save the workspace at any time in the session with the {{ic|save.image()}} command.
  
=== Installing R packages ===
+
===Variables===
There are many add-on R packages, which can be browsed on [http://cran.r-project.org/web/packages/available_packages_by_date.html The R Website.]. They can be installed from within R using the R install.packages command. It is not necessary to be superuser or have root privileges when doing this -- R can install its packages locally for the individual user. This is the safest way to install R packages and won't conflict with the pacman package management.
+
{{ic|R}} can be confusing when it comes to [[Environment Variables]].{{ic|R}} search for '''site''' (understand '''system wide''') and user files to process for setting them. Most important ones can be found on [http://stat.ethz.ch/R-manual/R-devel/library/base/html/EnvVar.html R Documentation]. The ''site'' {{ic|R_ENVIRON}} file is ${R_HOME}/etc/Renviron. To see {{ic|R_HOME}} variable, run the {{ic|Sys.getenv()}} command within your {{ic|R}} session: {{hc|> Sys.getenv("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 {{ic|R_ENVIRON_USER}} ''environment variable''. You can append this line in your [[Bashrc]]: {{bc|
 +
export R_HOME_USER=/path/to/your/Renviron}}
  
To set the location of your local R package library, create a {{ic|~/.Renviron}} file and append the following line:
+
{{ic|R}} packages are installed into ''libraries'' which are directories in the file system containing a subdirectory for each package installed there.
 +
To set the location of your local {{ic|R}} package library, append the following line in your {{ic|Renviron}} file:
 
{{bc|R_LIBS_USER=/path/to/directory/R/packages}}
 
{{bc|R_LIBS_USER=/path/to/directory/R/packages}}
 +
 +
 +
== Installing R packages ==
 +
There are many add-on {{ic|R}} packages, which can be browsed on [http://cran.r-project.org/web/packages/available_packages_by_date.html The R Website.]. They can be installed from within {{ic|R}} using the {{ic|'''install.packages(''pkgname'')''' command}}. {{ic|R}} can install its packages locally as '''per user''' local settings or '''system wide'''.
 +
 +
Within your {{ic|R }} session, run this command to check that your user library exists and is set correctly:
 +
{{hc|> Sys.getenv("R_LIBS_USER")|
 +
[1] "/path/to/directory/R/packages"}}
 +
Installation within your {{ic|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:
 +
 +
{{bc|$ R CMD INSTALL -l $R_LIBS_USER ''pkg1 pkg2 ...''}}
  
 
=== Upgrading R packages ===
 
=== Upgrading R packages ===
Line 32: Line 47:
 
Or when you also need to rebuild packages which were built for an older version:
 
Or when you also need to rebuild packages which were built for an older version:
 
  > update.packages(ask=FALSE,checkBuilt=TRUE)
 
  > update.packages(ask=FALSE,checkBuilt=TRUE)
 +
 +
==Configuration==
  
 
== Adding a graphical frontend to R ==
 
== Adding a graphical frontend to R ==

Revision as of 23:10, 3 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

The main main script is usr/bin/R, basic packages are found in $HOME/R/ and system-wide configuration files in /etc/R/.

Make sure R has been installed correctly by running the following command in a terminal to start a R session:

$ 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 >

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 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.R search for site (understand system wide) and user files to process for setting them. Most important ones can be found on R Documentation. The site R_ENVIRON file is ${R_HOME}/etc/Renviron. To see R_HOME variable, run the Sys.getenv() command within your R session:
> Sys.getenv("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. You can append this line in your Bashrc:
export R_HOME_USER=/path/to/your/Renviron

R packages are installed into libraries which are directories in the file system containing a subdirectory for each package installed there. To set the location of your local R package library, append the following line in your Renviron file:

R_LIBS_USER=/path/to/directory/R/packages


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

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.