R Setup - gizotso/R GitHub Wiki

R Setup

print(version) : R version (release, os, ...). R.version
print(sessionInfo()) : R session info, confirm attached packages

options()

memory.size(), memory.limit(), memory.limit(size=2000) # 2000MB object.size(), memory.profile()

Sys.setlocale(): linguistic and encoding options Sys.setenv(LANGUAGE='en')

R:.
β”œβ”€β”€β”€bin
β”‚   └───i386
β”‚   └───x64
β”‚   └───R.exe
β”‚   └───Rscript.exe
β”œβ”€β”€β”€doc
β”œβ”€β”€β”€etc
β”œβ”€β”€β”€include
β”œβ”€β”€β”€library
β”œβ”€β”€β”€modules
β”œβ”€β”€β”€share
β”œβ”€β”€β”€src
β”œβ”€β”€β”€Tcl
β”œβ”€β”€β”€tests
β”œβ”€β”€β”€...

Windows env variables

  • R_HOME : R directory
  • R_HOME_bin : where you find R executables (R.exe, Egui.exe,...). To be added to the PATH variable.
  • R_LIBS (optional): custom directory for R packages
  • R_LIBS_USER : automatically set to folder under user's home (ex: "C:/Users/family/Documents/R/win-library/3.2")
  • HOME : automatically set to user's home directory (ex: "C:/Users/family/Documents")
:: ------ SetEnv.bat -----
:: R setup (since R3.2 need to tweak to point correct version i386/x64)
set R_HOME=C:\Tools\R\R-3.2.5
IF "%PROCESSOR_ARCHITECTURE%"=="x86" (set R_HOME_bin=%R_HOME%\bin\i386) else (set R_HOME_bin=%R_HOME%\bin\x64)
set PATH=%R_HOME_bin%;%PATH%
:: Set en variables permanently for current user
:: (need to restart Cmd to be effective: echo %R_HOME%)
setx R_HOME "C:\Tools\R\R-3.2.5"
setx R_HOME_bin %R_HOME%\bin\x64
setx R_LIBS "C:\Tools\R\RLibs-3.2.5"

Environment Variables : .Renviron / Renviron.site

Setting environment variables (ignored if R invoked with --no-environ)

  • Renviron.site : At startup, Lookup : 1.$R_ENVIRON, 2. if unset/empty, $R_HOME/etc/Renviron.site. Note : this config file does not exists on a factory-fresh install
  • .Renviron :1. current dir, 2. user's home directory

.Renviron

## Linux
R_LIBS=~/R/library
## Windows
R_LIBS=C:/R/library

Accessing Environment within a R sesion

Sys.getenv('R_LIBS_USER')
Sys.getenv("R_HOME")
# [1] "T:/PortableApps/PortableApps/R-Portable/App/R-Portable"

normalizePath(Sys.getenv("R_HOME"))
# [1] "T:\\PortableApps\\PortableApps\\R-Portable\\App\\R-Portable"

R.home()
# [1] "C:/Tools/R/R-3.2.5"
R.home("doc")

# Library Search Path
.libPaths()
# add to Lib Path
.libPaths('T:\\SWDev\\lib'))

# Home dir
Sys.getenv("HOME")
path.expand("~")

Set env

Sys.setenv()
Sys.setlocale() # linguistic and encoding options
Sys.setenv(LANGUAGE='en')# set error message language

Startup script : Rprofile (.Rprofile / Rprofile.site)

https://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html

  • Rprofile.site (ignored if R invoked with --no-site-file) Lookup : $R_PROFILE, if unset try $R_HOME/etc/Rprofile.site. This code is loaded into package base
  • .Rprofile (ignored if R invoked with --no-init-file) Lookup : 1. current dir, 2. user's home directory and sources it into the user workspace. ? $R_PROFILE_USER if set
  • .First (.Rprofile or Rprofile.site or .Rdata) : if found on the search path, it is executed as .First()

R> example(startup) will provide Renviron and Rprofile examples
R> help(startup) will provide help
R> options () will show all options

Rprofile.site

# set a site library
# .Library.site <- file.path(chartr("\\", "/", R.home()), "site-library")
# if R_LIBS env variable is not set
# Sys.setenv(R_LIBS = "T:/PortableApps/R/R_lib_32")
# add R_LIBS to library Path
.libPaths(Sys.getenv('R_LIBS'))

# alias qq to exit without asking for saving
qq <- function() { q("no") }

Rprofile.site sample

# Sample Rprofile.site file
# ${R_HOME}/etc/Rprofile.site
# source : http://www.statmethods.net/interface/customizing.html

# Things you might want to change
# options(papersize="a4")
# options(editor="notepad")
# options(pager="internal")

# R interactive prompt
# options(prompt="> ")
# options(continue="+ ")

# to prefer Compiled HTML
help options(chmhelp=TRUE)
# to prefer HTML help
# options(htmlhelp=TRUE)

# General options
options(tab.width = 2)
options(width = 130)
options(graphics.record=TRUE)

.First <- function(){
 library(Hmisc)
 library(R2HTML)
 cat("\nWelcome at", date(), "\n")
}

.Last <- function(){
 cat("\nGoodbye at ", date(), "\n")
}
print(version)

print(.libPaths())
[1] "C:/Tools/R/RLibs-3.2.5"     "C:/Tools/R/R-3.2.5/library"

R Startup References

R IDE

Associate .R, .Rmd files to be opened with RStudio

Some utilities are packaged within RStudio : GnuGrep <$RStudio>\bin\gnugrep

R:.
β”œβ”€β”€β”€bin
β”‚   └───gnugrep
β”‚   └───gnudiff
β”‚   └───pandoc
β”‚   rstudio.exe
β”œβ”€β”€β”€R
β”‚   └───modules
β”‚    Options.R, ServerOptions.R, ...
β”œβ”€β”€β”€resources
β”œβ”€β”€β”€wwww
β”œβ”€β”€β”€www-symbolmaps

Others

Web interfaces for R / R Web Application

R >Ipython notebook

⚠️ **GitHub.com Fallback** ⚠️