Difference between revisions of "R"

From ArchWiki
Jump to navigation Jump to search
(→‎Within a R session: added list of official mirrors)
m (→‎Environment: The dot is for the user files, otherwise, the system files do not have the dot : "/etc/R/Renviron")
 
(29 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Category:Mathematics and science]]
+
[[Category:Science]]
 
[[Category:Programming languages]]
 
[[Category:Programming languages]]
 
[[ja:R]]
 
[[ja:R]]
 
{{Related articles start}}
 
{{Related articles start}}
 +
{{Related|R package guidelines}}
 
{{Related|Intel C++}}
 
{{Related|Intel C++}}
 
{{Related articles end}}
 
{{Related articles end}}
[http://www.r-project.org/ R] is a "free software environment for statistical computing and graphics (http://www.r-project.org/)."
+
[[Wikipedia:R (programming language)|R]] is a "free software environment for statistical computing and graphics."[https://www.r-project.org/]
  
 
== Installation ==
 
== Installation ==
=== Basic package ===
 
  
 
[[Install]] the {{Pkg|r}} package. The installation of external packages within the R environment may require {{pkg|gcc-fortran}}.
 
[[Install]] the {{Pkg|r}} package. The installation of external packages within the R environment may require {{pkg|gcc-fortran}}.
  
=== Initial configuration ===
+
== Usage ==
 
 
Please refer to [http://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html Initialization at Start of an R Session] to get a detailed understanding of startup process.
 
The home directory of the {{ic|R}} installation is {{ic|/usr/lib/R}}. Base packages are found in {{ic|/usr/lib/R/library/base}} and '''site''' configuration files in {{ic|/etc/R/}}. Aspects of the [[Locale]] are accessed by the functions {{ic|Sys.getlocale}} and {{ic|Sys.localeconv}} within the {{ic|R}} session. Locales will be the one defined in your system.
 
  
 
To start a {{ic|R}} session, open your terminal and type this command:
 
To start a {{ic|R}} session, open your terminal and type this command:
Line 27: Line 24:
  
 
{{Tip|  
 
{{Tip|  
* Tired of R's verbose startup message ? Then start {{ic|R}} with the {{ic|--quiet}} command-line option ({{ic|$ R --quiet}}). You can {{ic|alias R="R --quiet"}} in one of your [[Startup files]].
+
* Tired of R's verbose startup message ? Then start {{ic|R}} with the {{ic|--quiet}} command-line option ({{ic|$ R --quiet}}). You can add {{ic|1=alias R="R --quiet"}} in one of your [[Startup files]].
 
* Running {{ic|R}} from the command line will set R's working directory to the current directory. Opening the R GUI will set R's working directory to $HOME, unless explicitly defined in your configuration files ({{ic|.Renviron}} or {{ic|.Rprofile}}).
 
* Running {{ic|R}} from the command line will set R's working directory to the current directory. Opening the R GUI will set R's working directory to $HOME, unless explicitly defined in your configuration files ({{ic|.Renviron}} or {{ic|.Rprofile}}).
* To colorize R output, first install the {{ic|colorout}} package (first do {{ic|install.packages("devtools")}} if {{ic|devtools}} package is not installed)...
 
> devtools::install_github('jalvesaq/colorout')
 
...and then add the following to your {{ic|.Rprofile}} (by default, located in {{ic|$HOME/.Rprofile}}; create it if it does not exist).
 
# $HOME/.Rprofile
 
 
if (interactive()) {
 
  # Everything in here is only run if R is in "interactive" (as opposed to batch/script) mode
 
  library(colorout)
 
}
 
 
}}
 
}}
  
===Variables===
+
== Configuration ==
{{ic|R}} can be confusing when it comes to [[environment variables]], as they are large and duplicated following the '''site''' or '''user''' sides. There are two sorts of files used in startup: ''environment files'', defined by {{ic|$R_ENVIRON}} and ''profile files'', defined by {{ic|$R_PROFILE}}.
+
 
 +
Whenever R starts, its configuration is controlled by several files.
 +
Please refer to [http://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html Initialization at Start of an R Session] to get a detailed understanding of startup process.
 +
 
 +
=== Environment ===
 +
 
 +
R first loads '''site''' and '''user''' environment variable files.
 +
The name of the site file is controlled by the [[Environment variables]] {{ic|R_ENVIRON}} if it exists, and defaults to {{ic|/etc/R/Renviron}}.
 +
The name of the user file is specified by {{ic|R_ENVIRON_USER}}.
 +
If that is unset, it defaults to {{ic|.Renviron}} in the curent working directory or if it exists, and {{ic|~/.Renviron}} otherwise.
  
 
Most important variables can be found on [http://stat.ethz.ch/R-manual/R-devel/library/base/html/EnvVar.html Environment Variables R Documentation].
 
Most important variables can be found on [http://stat.ethz.ch/R-manual/R-devel/library/base/html/EnvVar.html Environment Variables R Documentation].
  
====R_ENVIRON====
+
You may disable loading environment files with {{ic|--no-environ}}
At startup, {{ic|R}} search at early stage for '''site''' and '''user''' {{ic|.Renviron}} files to process for setting [[environment variables]]. The '''site''' file is located in {{ic|/etc/R}}, and generated by configure.
+
 
 +
Lines in {{ic|Renviron}} file should be either comment lines starting with '''#''' or lines of the form ''name=value''.
 +
Here is a very basic {{ic|.Renviron}}:
  
The name of the user file can be specified by the {{ic|R_ENVIRON_USER}} environment variable.
+
{{hc|1=.Renviron|2=
If you do not specify any file, {{ic|R}} will automatically read {{ic|.Renviron}} in your home directory if there is one. In case you want to use another emplacement for this file, append this line {{ic| export R_ENVIRON_USER ="path/to/.Renviron"}} in one of your [[startup files]]. This is the place to set all kind of environment variables using the '''R syntax'''.
+
R_HOME_USER = /path/to/your/r/directory
 +
R_PROFILE_USER = ${HOME}/.config/r/.Rprofile
 +
R_LIBS_USER = /path/to/your/r/library
 +
R_HISTFILE = /path/to/your/filename.Rhistory                                            # Do not forget to append the '''.Rhistory'''
 +
MYSQL_HOME = /var/lib/mysql                 
 +
}}
  
====R_PROFILE====
+
=== Profile ===
Then {{ic|R}} searches for the '''site-wilde''' {{ic|Rprofile.site}} defined by the {{ic|R_PROFILE}} environment variable. This file does not exist after a fresh installation.
 
Finally, {{ic|R}} seraches for '''user''' {{ic|R_PROFILE_USER}}. if unset, a file called {{ic|.Rprofile}} is searched for in the current directory, returned by the {{ic|R}} command {{ic|> getwd()}} or in the user's home directory. This is the place to put all your custom {{ic|R}} code.
 
  
== Installing R packages ==
+
R then loads an Rprofile, which contains R code that is executed.
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(c("pkgname"))'''}} command. {{ic|R}} can install its packages locally as '''per user''' local settings or '''system wide'''.
+
These files are read in the following order of preference (only one file is loaded):
  
{{Note|
+
1. A file specified by the environment variable {{ic|R_PROFILE_USER}}.
* {{ic|'''install.packages()'''}} requires {{Pkg|tk}} to be [[installed]] for selecting mirrors. Try installing this package if you see:
 
  
{{ic|Error: .onLoad failed in loadNamespace() for 'tcltk', details (...) }}
+
2. {{ic|.Rprofile}} in the current working directory.
  
}}
+
3. {{ic|$HOME/.Rprofile}}.
 
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 will not 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 ...''}}
+
An {{ic|.Rprofile}} can contain arbitrary R code, though best practice suggests that one should not load packages at startup, as this hinders package upgrades and reproducibility.
  
=== Upgrading R packages ===
+
{{hc|1=~/.Rprofile|2=
==== Within a R session ====
+
# The .First function is called after everything else in .Rprofile is executed
> update.packages(ask=FALSE)
+
.First <- function() {
Or when you also need to rebuild packages which were built for an older version:
+
  # Print a welcome message
> update.packages(ask=FALSE,checkBuilt=TRUE)
+
  message("Welcome back ", Sys.getenv("USER"),"!\n","working directory is:", getwd())
Or when you also need to specify a specific mirror (https://cran.r-project.org/mirrors.html) to download the packages from (changing the url as need):
+
}
> update.packages(ask=FALSE,checkBuilt=TRUE,repos="https://cran.cnr.berkeley.edu/")
 
{{Tip|upgrading packages from your '''R''' session can quickly be a pain if you have too many loaded packages at start up. For packages to be upgraded, they need to be ''not loaded''. Best practice is to start a new clean R session this way: {{ic| R --vanilla}} then run the upgrade.
 
}}
 
  
==== Within a shell ====
+
options(digits = 12)                                          # number of digits to print. Default is 7, max is 15
First, [[Install]] {{AUR|littler}} from the [[Arch User Repository]]. This package allows direct execution of '''R''' commands and can be seen as a scripting front-end. Then, visit [http://dirk.eddelbuettel.com/code/littler.examples.html dirk.eddelbuettel] webiste about '''littler'''. Below is the script to run packages updates:
+
options(stringsAsFactors = FALSE)                            # Disable default conversion of character strings to factors
{{bc|
+
options(show.signif.stars = FALSE)                            # Don't show stars indicating statistical significance in model outputs
#!/usr/bin/env r
+
error <- quote(dump.frames("${R_HOME_USER}/testdump", TRUE))  # post-mortem debugging facilities
#
 
# a simple example to update packages in /usr/local/lib/R/site-library
 
# parameters are easily adjustable
 
  
repos <- "http://cran.rstudio.com"
+
}}
  
lib.loc <- Sys.getenv("R_LIBS_USER")
+
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.
 +
See this [http://stackoverflow.com/questions/1189759/expert-r-users-whats-in-your-rprofile post] for more examples of user configurations.
  
update.packages(repos&#61;repos, ask&#61;FALSE, lib.loc&#61;lib.loc)
+
=== Locale ===
}}
 
  
Put this script in your favorite place and make it executable. Of course, feel free to modify the repo URL and the library location.
+
Aspects of the [[Locale]] are accessed by the functions {{ic|Sys.getlocale}} and {{ic|Sys.localeconv}} within the {{ic|R}} session. Locales will be the one defined in your system.
  
{{Warning|when using [[zsh]], '''r''' is a builtin command. You will have to either use an explicit path {{ic|/usr/bin/r}} or create an alias under a different name.}}
+
== Managing 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.].
  
==== Automatically after R upgrades ====
+
{{Note|
R packages may not be compatible between R versions. You may want to automatically upgrade them and rebuild them if necessary when R upgrades. This can be accomplished with a pacman hook:
+
Some R packages link to files provided by system packages. These packages will need to be reinstalled when these files are updated.}}
  
{{hc|/etc/pacman.d/hooks/r-upgrade.hook|<nowiki>
+
=== With pacman ===
[Trigger]
+
There are some packages available on the [[AUR]] with the prefix {{ic|r-}}. You can mix and match installing R packages with pacman and through R (below), but if you do so you should let pacman manage system packages (those that reside at {{ic|/usr/lib/R/library}}, and let R manage user-installed packages elsewhere (e.g. {{ic|~/R/library}}).
Operation = Upgrade
 
Type = Package
 
Target = r
 
  
[Action]
+
=== With R ===
Description = Updates R packages and rebuilds them if necessary after R upgrade
+
Packages can be installed from within {{ic|R}} using the {{ic|'''install.packages(c("pkgname"))'''}} command. You should use a local library and let pacman manage files that reside under {{ic|/usr/lib/R/library}}.
# Depends is optional if this should depend on another package
 
Depends =  
 
When = PostTransaction
 
Exec = Rscript -e 'update.packages(checkBuilt = TRUE, ask = FALSE)'
 
</nowiki>}}
 
  
==Configuration files==
+
{{Note|
 +
* {{ic|'''install.packages()'''}} requires {{Pkg|tk}} to be [[install]]ed for selecting mirrors. Try installing this package if you see:
  
The two user configuration files you want in your home folder are {{ic|.Renviron}} and {{ic|Rprofile.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, put the {{ic|Rprofile.r}} file at the root of the directory and append all your {{ic|R}} code  in this file.
+
{{ic|Error: .onLoad failed in loadNamespace() for 'tcltk', details (...) }}
===.Renviron===
 
Lines in {{ic|Renviron}} file should be either comment lines starting with '''#''' or lines of the form ''name&#61;value''.Here is a very basic {{ic|.Renviron}} you can start with:
 
{{hc|.Renviron|                                     
 
R_HOME_USER &#61; /path/to/your/r/directory
 
R_PROFILE_USER &#61; ${HOME}/.config/r/Rprofile.r
 
R_LIBS_USER &#61; /path/to/your/r/library
 
R_HISTFILE &#61; /path/to/your/filename.Rhistory                                            # Do not forget to append the '''.Rhistory'''
 
R_DEFAULT_PACKAGES &#61; 'datasets,utils,grDevices,graphics,stats,methods,rJava,colorout'    # load some default packages at start up
 
MYSQL_HOME &#61; /var/lib/mysql                 
 
}}
 
  
===Rprofile===
+
* Alternatively, you can disable graphical pop-ups like this by running [https://stackoverflow.com/q/7430452/1358308]:
The file {{ic|.Rprofile}} contains R code that is run automatically at the beginning of each R session. These files are read in the following order of preference (only one file is loaded):
 
  
1. {{ic|.Rprofile}} in the current working directory.
+
{{ic|1=> options(menu.graphics=FALSE)}}
  
2. User {{ic|.Rprofile}}, typically in {{ic|$HOME/.Rprofile}}.
+
to make this change more permanent add the above line to your Rprofile.
 +
}}
 +
 +
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"}}
  
3. Site {{ic|.Rprofile}}, typically in {{ic|/etc/.Rprofile}}.
+
Alternatively, you may install from the command line like so:
  
Here is an example {{ic|.Rprofile}} that defines some useful settings:
+
{{bc|$ R CMD INSTALL -l $R_LIBS_USER ''pkg1 pkg2 ...''}}
  
{{hc|~/.Rprofile|
+
==== Upgrading R packages ====
# The .First function is called after everything else in .Rprofile is executed
+
===== Within a R session =====
.First <- function() {
+
> update.packages(ask=FALSE)
  # Print a welcome message
+
Or when you also need to rebuild packages which were built for an older version:
  message("Welcome back ", Sys.getenv("USER"),"!\n","working directory is:", getwd())
+
> update.packages(ask=FALSE,checkBuilt=TRUE)
}
+
Or when you also need to select a specific mirror (https://cran.r-project.org/mirrors.html) to download the packages from (changing the url as need):
 +
> update.packages(ask=FALSE,checkBuilt=TRUE,repos="https://cran.cnr.berkeley.edu/")
 +
{{Tip|upgrading packages from your R session can quickly be a pain if you have too many loaded packages at start up. For packages to be upgraded, they cannot be loaded, so do not load packages from your Rprofile.
 +
}}
  
# Always load the 'methods' package
+
===== Within a shell =====
library(methods)
+
You can use {{ic|Rscript}}, which comes with {{Pkg|r}} to update packages from a shell:
  
# Load the devtools and colorout packages if in interactive mode
+
{{bc|
if (interactive()) {
+
$ Rscript -e "update.packages()"}}
  library(devtools)
 
  library(colorout)
 
}
 
  
options(prompt &#61; paste(paste(Sys.info()[c("user", "nodename")], collapse &#61; "@"),"[R] "))            # customize your R prompt with username and hostname in this format: user@hostname [R]
 
options(digits &#61; 12)                                                                                # number of digits to print. Default is 7, max is 15
 
options(stringsAsFactors &#61; FALSE)                                                                  # Disable default conversion of character strings to factors
 
options(show.signif.stars &#61; FALSE)                                                                  # Don't show stars indicating statistical significance in model outputs
 
error <- quote(dump.frames("${R_HOME_USER}/testdump", TRUE))                                        # post-mortem debugging facilities
 
  
 +
===Makevars===
 +
The Makevars file can be used to set the default make options when installing packages. An example optimized Makevars file is as follow:
 +
{{hc|~/.R/Makevars|2=
 +
CFLAGS=-O3 -Wall -pedantic -march=native -mtune=native -pipe
 +
CXXFLAGS=-O3 -Wall -pedantic -march=native -mtune=native -pipe
 
}}
 
}}
 
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.
 
See this [http://stackoverflow.com/questions/1189759/expert-r-users-whats-in-your-rprofile post] for more examples of user configurations.
 
  
 
== Adding a graphical frontend to R ==
 
== Adding a graphical frontend to R ==
Line 175: Line 150:
  
 
=== R Commander frontend ===
 
=== 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 {{Pkg|tk}} to be [[installed]].
+
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 {{Pkg|tk}} to be [[install]]ed.
  
 
To install R Commander, run 'R' from the command line. Then type:
 
To install R Commander, run 'R' from the command line. Then type:
Line 207: Line 182:
  
 
=== Emacs Speaks Statistics ===
 
=== Emacs Speaks Statistics ===
{{Pkg|emacs}} users can interact with R via the {{AUR|emacs-ess}} package.
+
[[Emacs]] users can interact with R via the {{AUR|emacs-ess}} package.
  
 
=== Nvim-R ===
 
=== Nvim-R ===
Line 220: Line 195:
 
== Tips and tricks ==
 
== Tips and tricks ==
  
 +
=== Optimized packages ===
 +
The numerical libraries that comes with the R (generic {{Pkg|blas}}, LAPACK) do not have multithreading capabilities. Replacing the reference {{Pkg|blas}} package with an optimized BLAS can produce dramatic speed increases for many common computations in R. See these threads for an overview of the potential speed increases:
  
=== Optimized packages ===
+
* https://github.com/tmolteno/necpp/issues/18
The numerical libraries that comes with the R (generic {{Pkg|blas}}, LAPACK) do not have multithreading capabilities. Replacing the reference {{Pkg|blas}} package with an optimized BLAS can produce dramatic speed increases for many common computations in R. However the available optimized BLAS packages all have drawbacks such as requiring a commercial license or [http://blog.revolutionanalytics.com/2015/10/edge-cases-in-using-the-intel-mkl-and-parallel-programming.html potentially interfering with the standard R functionality for parallel processing]. If you really need faster linear algebra in R you may consider the following options.
+
* http://blog.nguyenvq.com/blog/2014/11/10/optimized-r-and-python-standard-blas-vs-atlas-vs-openblas-vs-mkl/
 +
* https://freddie.witherden.org/pages/blas-gemm-bench/
 +
* http://nghiaho.com/?p=1726
  
 
==== OpenBLAS ====
 
==== OpenBLAS ====
{{AUR|openblas}} can be installed from the [[AUR]], replacing the reference {{Pkg|blas}} from extra. If you are using the regular {{Pkg|r}} package from extra no further configuration is needed; R is configured to use the system BLAS and will use OpenBLAS once it is installed.
+
{{Pkg|openblas}} can replace the reference {{Pkg|blas}}. If you are using the regular {{Pkg|r}} package from [extra] no further configuration is needed; R is configured to use the system BLAS and will use OpenBLAS once it is installed.
 +
 
 
==== Intel MKL ====
 
==== Intel MKL ====
'''If your processors are Intel''', you can use the [http://software.intel.com/en-us/intel-mkl Intel math Kernel Library]. The '''MKL''', beyond the capabilities of multithreading, also has specific optimizations for Intel processors.
+
'''If your processors are Intel''', you can use the [http://software.intel.com/en-us/intel-mkl Intel math Kernel Library]. The '''MKL''', beyond the capabilities of multithreading, also has specific optimizations for Intel processors. Keep in mind that they can [http://blog.revolutionanalytics.com/2015/10/edge-cases-in-using-the-intel-mkl-and-parallel-programming.html potentially interfere with the standard R functionality for parallel processing].
  
 
Please first [[Install]] the {{AUR|intel-mkl}} package available from [[AUR]], then the {{AUR|r-mkl}} package.
 
Please first [[Install]] the {{AUR|intel-mkl}} package available from [[AUR]], then the {{AUR|r-mkl}} package.
  
{{Note|  
+
{{Note|
 
* if you install the {{AUR|r-mkl}} with '''R''' already installed, you will be prompted to remove '''R'''. Once '''r-mkl''' is installed, please run on '''R''' console the following command :
 
* if you install the {{AUR|r-mkl}} with '''R''' already installed, you will be prompted to remove '''R'''. Once '''r-mkl''' is installed, please run on '''R''' console the following command :
{{ic|> update.packages(checkBuilt&#61;TRUE)}}
+
{{ic|1=> update.packages(checkBuilt=TRUE)}}
 
* here are elapsed time in sec from computing 15 tests with default GCC build and icc/MKL build: ''274.93 sec'' for GCC build, ''21.01 sec'' for icc/MKL build. See [https://stat.ethz.ch/pipermail/r-help/2014-September/421574.html this post] for more information.
 
* here are elapsed time in sec from computing 15 tests with default GCC build and icc/MKL build: ''274.93 sec'' for GCC build, ''21.01 sec'' for icc/MKL build. See [https://stat.ethz.ch/pipermail/r-help/2014-September/421574.html this post] for more information.
 
}}
 
}}
Line 256: Line 236:
 
}}
 
}}
  
 +
== See also ==
  
== Troubleshooting ==
+
* [https://www.r-project.org/ Official website]
 
 
=== Package PKI won't install ===
 
The version of the PKI package currently on CRAN (0.1-3) is old and will not work with the current version of {{Pkg|openssl}}. The work around is to install a newer version from rforge:
 
 
 
> install.packages("PKI", repos = "https://www.rforge.net")
 
 
 
== See also ==
 
 
* [http://www.rseek.org/ RSeek] A Google Custom Search Engine for R related material.
 
* [http://www.rseek.org/ RSeek] A Google Custom Search Engine for R related material.
 
* [http://r4ds.had.co.nz/ R for Data Science] Online version of a CCA licensed book written by Garrett Grolemund and Hadley Wickham from RStudio, 2017.
 
* [http://r4ds.had.co.nz/ R for Data Science] Online version of a CCA licensed book written by Garrett Grolemund and Hadley Wickham from RStudio, 2017.
 
* [https://www.r-bloggers.com/ R-bloggers] Aggregation site for (English) blogs related to R.
 
* [https://www.r-bloggers.com/ R-bloggers] Aggregation site for (English) blogs related to R.
 
* [https://www.reddit.com/r/Rlanguage/ /r/Rlanguage on Reddit] There are several R related Subreddits, each one provides links to the others.
 
* [https://www.reddit.com/r/Rlanguage/ /r/Rlanguage on Reddit] There are several R related Subreddits, each one provides links to the others.

Latest revision as of 12:13, 16 August 2019

R is a "free software environment for statistical computing and graphics."[1]

Installation

Install the r package. The installation of external packages within the R environment may require gcc-fortran.

Usage

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

$ R
Note:
  • Make sure to use a capital R for the command. Note that some shells use the lowercase r command 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 ?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, functions. The saved image is stored in .RData format 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(image.RData) command, save as many images as you want (eg : image1.RData, image2.RData). You can load image with the load.image(image.RData) command at any time of your session.

Tip:
  • Tired of R's verbose startup message ? Then start R with the --quiet command-line option ($ R --quiet). You can add alias R="R --quiet" in one of your Startup files.
  • Running R from the command line will set R's working directory to the current directory. Opening the R GUI will set R's working directory to $HOME, unless explicitly defined in your configuration files (.Renviron or .Rprofile).

Configuration

Whenever R starts, its configuration is controlled by several files. Please refer to Initialization at Start of an R Session to get a detailed understanding of startup process.

Environment

R first loads site and user environment variable files. The name of the site file is controlled by the Environment variables R_ENVIRON if it exists, and defaults to /etc/R/Renviron. The name of the user file is specified by R_ENVIRON_USER. If that is unset, it defaults to .Renviron in the curent working directory or if it exists, and ~/.Renviron otherwise.

Most important variables can be found on Environment Variables R Documentation.

You may disable loading environment files with --no-environ

Lines in Renviron file should be either comment lines starting with # or lines of the form name=value. Here is a very basic .Renviron:

.Renviron
R_HOME_USER = /path/to/your/r/directory
R_PROFILE_USER = ${HOME}/.config/r/.Rprofile
R_LIBS_USER = /path/to/your/r/library
R_HISTFILE = /path/to/your/filename.Rhistory                                             # Do not forget to append the .Rhistory
MYSQL_HOME = /var/lib/mysql

Profile

R then loads an Rprofile, which contains R code that is executed. These files are read in the following order of preference (only one file is loaded):

1. A file specified by the environment variable R_PROFILE_USER.

2. .Rprofile in the current working directory.

3. $HOME/.Rprofile.

An .Rprofile can contain arbitrary R code, though best practice suggests that one should not load packages at startup, as this hinders package upgrades and reproducibility.

~/.Rprofile
# The .First function is called after everything else in .Rprofile is executed
.First <- function() {
  # Print a welcome message
  message("Welcome back ", Sys.getenv("USER"),"!\n","working directory is:", getwd())
}

options(digits = 12)                                          # number of digits to print. Default is 7, max is 15
options(stringsAsFactors = FALSE)                             # Disable default conversion of character strings to factors
options(show.signif.stars = FALSE)                            # Don't show stars indicating statistical significance in model outputs
error <- quote(dump.frames("${R_HOME_USER}/testdump", TRUE))  # post-mortem debugging facilities

You can add more global options to customize your R environment. See this post for more examples of user configurations.

Locale

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.

Managing R packages

There are many add-on R packages, which can be browsed on The R Website..

Note: Some R packages link to files provided by system packages. These packages will need to be reinstalled when these files are updated.

With pacman

There are some packages available on the AUR with the prefix r-. You can mix and match installing R packages with pacman and through R (below), but if you do so you should let pacman manage system packages (those that reside at /usr/lib/R/library, and let R manage user-installed packages elsewhere (e.g. ~/R/library).

With R

Packages can be installed from within R using the install.packages(c("pkgname")) command. You should use a local library and let pacman manage files that reside under /usr/lib/R/library.

Note:
  • install.packages() requires tk to be installed for selecting mirrors. Try installing this package if you see:

Error: .onLoad failed in loadNamespace() for 'tcltk', details (...)

  • Alternatively, you can disable graphical pop-ups like this by running [2]:

> options(menu.graphics=FALSE)

to make this change more permanent add the above line to your Rprofile.

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"

Alternatively, you may install from the command line like so:

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

Upgrading R packages

Within a R session
> 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)

Or when you also need to select a specific mirror (https://cran.r-project.org/mirrors.html) to download the packages from (changing the url as need):

> update.packages(ask=FALSE,checkBuilt=TRUE,repos="https://cran.cnr.berkeley.edu/")
Tip: upgrading packages from your R session can quickly be a pain if you have too many loaded packages at start up. For packages to be upgraded, they cannot be loaded, so do not load packages from your Rprofile.
Within a shell

You can use Rscript, which comes with r to update packages from a shell:

$ Rscript -e "update.packages()"


Makevars

The Makevars file can be used to set the default make options when installing packages. An example optimized Makevars file is as follow:

~/.R/Makevars
CFLAGS=-O3 -Wall -pedantic -march=native -mtune=native -pipe
CXXFLAGS=-O3 -Wall -pedantic -march=native -mtune=native -pipe

Adding a graphical frontend to R

R does not include a point-and-click graphical user interface for statistics or data manipulation. 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 to be installed.

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.

Editors IDEs and notebooks with R support

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.

Rstudio server

RStudio Server enables you to provide a browser based interface to a version of R running on a remote Linux server.

Install rstudio-server-gitAUR. The two main configuration files are /etc/rstudio/rserver.conf and /etc/rstudio/rsession.conf. They are not created during the install, so you will need to create and edit them. For information about configure options, please refer to rstudio getting started documentation.

To start the server, please enable and start the rstudio-server.service unit file provided with the package.

Emacs Speaks Statistics

Emacs users can interact with R via the emacs-essAUR package.

Nvim-R

The nvim-rAUR package allows vim and neovim users to code in R, including editing and rendering of R markdown (Rmd) files, execution of R code in a separate pane, inspection of variables, and integrated help panes.

Cantor

cantor is a notebook application developed by KDE that includes support for R.

Jupyter notebook

jupyter-notebook is a browser based notebook with support for many programming languages. R support can be added by installing the IRkernel.

Tips and tricks

Optimized packages

The numerical libraries that comes with the R (generic blas, LAPACK) do not have multithreading capabilities. Replacing the reference blas package with an optimized BLAS can produce dramatic speed increases for many common computations in R. See these threads for an overview of the potential speed increases:

OpenBLAS

openblas can replace the reference blas. If you are using the regular r package from [extra] no further configuration is needed; R is configured to use the system BLAS and will use OpenBLAS once it is installed.

Intel MKL

If your processors are Intel, you can use the Intel math Kernel Library. The MKL, beyond the capabilities of multithreading, also has specific optimizations for Intel processors. Keep in mind that they can potentially interfere with the standard R functionality for parallel processing.

Please first Install the intel-mklAUR package available from AUR, then the r-mklAUR package.

Note:
  • if you install the r-mklAUR with R already installed, you will be prompted to remove R. Once r-mkl is installed, please run on R console the following command :

> update.packages(checkBuilt=TRUE)

  • here are elapsed time in sec from computing 15 tests with default GCC build and icc/MKL build: 274.93 sec for GCC build, 21.01 sec for icc/MKL build. See this post for more information.

intel-advisor-xe

intel-advisor delivers top application performance with C, C++ and Fortran compilers, libraries and analysis tools.

Install the intel-advisor-xeAUR[broken link: package not found] package.

Set CRAN mirror across R sessions

Instead of having R ask which CRAN mirror to use every time you install or update a package, you can set the mirror in the Rprofile file. https://cloud.r-project.org/ should be a good default for everywhere:

~/.Rprofile
## Set CRAN mirror:
local({
  r <- getOption("repos")
  r["CRAN"] <- "https://cloud.r-project.org/"
  options(repos = r)
})

See also

  • Official website
  • RSeek A Google Custom Search Engine for R related material.
  • R for Data Science Online version of a CCA licensed book written by Garrett Grolemund and Hadley Wickham from RStudio, 2017.
  • R-bloggers Aggregation site for (English) blogs related to R.
  • /r/Rlanguage on Reddit There are several R related Subreddits, each one provides links to the others.