Setting System Options in R

Some Current System Settings

Some settings can be found by running the following in an R session.
#  installed version of R
print(R.Version())

#  system information
print(Sys.info())

#  known package repositories
print(options()$repos)

#  package type to download for installing/updating
print(options()$pkgType)

#  known library locations
print(.libPaths())

Changing System Settings

System settings can be changed by running the following in an R session. Note that this will only change the settings for the current R session. See Saving Changed Options to save for future R sessions.
#  add sslib repository to front of existing list
options(repos=c("ftp://ftp.gns.cri.nz/pub/davidh/sslib/r-repo",
                options()$repos))

#  set package type for installing/updating
if (Sys.info()["sysname"]=="Windows") pkg.type <- "win.binary"
if (Sys.info()["sysname"]=="Linux")   pkg.type <- "source"

#  add a local library location (edit path below appropriately)
#  you need to create the directory first
#  can be useful if you do not have admin rights
#  see help page for .libPaths() for more details
if (Sys.info()["sysname"]=="Windows") .libPaths("c:/My Stuff/R-local/libs")
if (Sys.info()["sysname"]=="Linux")   .libPaths("/home/david/R-local/libs")

Note the use of / in the Windows paths too. R is developed in a UNIX environment where \ is used as a control character. This convention is also consistent with internet URLs.

Saving Changed Options

Changed system settings can be saved in an R profile file, further details in topic Startup in the R base package. One firstly creates an R script as above.

One needs to distinguish whether the revised settings are for all users of the computer (system), or just personal.

  1. If system settings for a Debian (UNIX) environment, the R script will be stored in
    /etc/R/Rprofile.site
    
    Other UNIX environments will have a similar location.
  2. If personal settings for a UNIX like environment, the R script will be stored in your home directory, e.g.
    /home/david/.Rprofile
    
  3. If system settings for a Windows environment, the R script (for R version 3.4.1) will be stored in something like (depending where you installed R):
    C:\Program Files\R\R-3.4.1\etc\Rprofile.site
    
  4. If personal settings for a Windows environment, the R script will be stored in a file called
    .Rprofile
    
    in the current directory, or home directory; see topic Startup in the R base package for more discussion.

System Environment Variables

Some of the above options can be made, alternatively, by setting or resetting the system environment variables. For an overview of the settings that R uses at startup, see the topic Startup in the R base package. A list of recognised environment variables is given in topic EnvVar in the R base package.

Environment variables can be set within R using the function Sys.setenv, which is also part of the R base package. The environment variables can be stored in a number of places, so things could become messy if one is not systematic. For example, in a UNIX bash like environment, personal user libraries could be defined by placing

export R_LIBS_USER="/home/david/R-Local"
into the .bashrc file. The same can be achieved in Windows 10:
Control Panel → System → Advanced System Settings → Environment Variables
Insert here, e.g. Variable=R_LIBS_USER, Value=C:\R\libs