2.9 R Startup
Sys.getenv(x)
get the values of the environment variables. Returns a vector of the same length as x
.
x
a character vector
Environment Variables examples:
> Sys.getenv(c("HOME", "R_HOME", "R_PAPERSIZE", "R_PRINTCMD"))
HOME R_HOME
"/Users/menghan" "/Library/Frameworks/R.framework/Resources"
R_PAPERSIZE R_PRINTCMD
"a4" "lpr"
Rstudio doesnn’t load Rprofile or Renviron
I store my Rprofile
and Renviron
in non-default places (i.e. ~/.config/R
). When opening R
in a normal shell, my environment is loaded perfectly fine. When opening Rstudio, it doesn’t load my options, settings or paths.
Have to wrap your option settings in
rstudio.sessionInit
https://damien-datasci-blog.netlify.app/post/2020-12-31-pimp-your-r-startup-message/
Open
.Rprofile
wrap up your options in the following snippet
Understanding R’s startup
https://rviews.rstudio.com/2017/04/19/r-for-enterprise-understanding-r-s-startup/
https://docs.posit.co/ide/user/ide/guide/environments/r/managing-r.html
usethis
is a workflow package: it automates repetitive tasks that arise during project setup and development, both for R packages and non-package projects.
2.9.1 .Rprofile
What is .Rprofile
?
.Rprofile
is a startup file to set options and environment variables. .Rprofile
files can be either at the user or project level.
- User-level
.Rprofile
files live in the base of the user’s home directory, and - project-level
.Rprofile
files live in the base of the project directory.
R will source only one .Rprofile
file. If there is a project-level .Rprofile
, the user-level file will NOT be sourced, i.e., the project-level config file take priority.
So if you have both a project-specific .Rprofile
file and a user .Rprofile
file that you want to use, you explicitly source the user-level .Rprofile
at the top of your project-level .Rprofile
with source("~/.Rprofile")
.
.Rprofile
files are sourced as regular R code, so setting environment variables must be done inside a Sys.setenv(key = "value")
call.
Quitting R will erase the default theme setting. If you load ggplot2
in a future session it will revert to the default gray theme. If you’d like for ggplot2
to always use a different theme (either yours or one of the built-in ones), you can set a load hook and put it in your .Rprofile
file. For example, the following hook sets the default theme to be theme_minimal()
every time the ggplot2
package is loaded.
Of course, you can always override this default theme by adding a theme object to any of your plots that you construct in ggplot2
.
2.9.2 .Renviron
.Renviron
is a user-controllable file that can be used to create environment variables. This is especially useful to avoid including credentials like API keys inside R scripts. This file is written in a key-value format, so environment variables are created in the format:
Key1=value1
Key2=value2
...additional key=value pairs
And then Sys.getenv("Key1")
will return "value1"
in an R session.
Like with the .Rprofile
file, .Renviron
files can be at either the user or project level. If there is a project-level .Renviron
, the user-level file will not be sourced. The usethis package includes a helper function for editing .Renviron
files from an R session with usethis::edit_r_environ()
.
The .Renviron
file is most useful for defining sensitive information such as API keys (such as GitHub, Twitter, or Posit Connect) as well as R specific environment variables like the history size (R_HISTSIZE=100000
) and default library locations R_LIBS_USER
.
Rcpp compilation breaks in R 4.1.0
devtools::build("my_package")
Error: Could not find tools necessary to compile a package
Call `pkgbuild::check_build_tools(debug = TRUE)` to diagnose the problem.
In RStudio, I am continually prompted to install additional build tools and I can’t install the build tool. \(\rightarrow\) Bypass the option
options(buildtools.check = function(action) TRUE)
.Turns out R was pointing to an old clang version in my Makevars.
I just deleted it using [in Terminal]
Install SDK command line tool
Download from developer.apple.com. Software development kit.
https://developer.apple.com/download/all/
R compiler tools for cpp on MacOS
https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/
install OpenMP enabled
clang
from the terminal