2.9 R Startup
Sys.getenv(x) get the values of the environment variables. Returns a vector of the same length as x.
xa 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 doesn’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.sessionInithttps://damien-datasci-blog.netlify.app/post/2020-12-31-pimp-your-r-startup-message/
Open
.Rprofilewrap 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
.Rprofilefiles live in the base of the user’s home directory, and - project-level
.Rprofilefiles 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 takes 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.
Here is an example project-level .Rprofile file that
- sources the user-level
.Rprofilefile, - load some commonly used packages, and
- optionally, sets some options and environment variables specific to the project.
cat("This is the local user .Rprofile file\n")
# Source the global user .Rprofile
source("~/.Rprofile")
# Add project-specific configurations below
library(knitr)
library(tidyverse)This can be convenient for loading commonly used packages in some documentation projects for yourself.
- Note that it is recommended to load packages individually and explicitly if you are working on a research project, where reproducibility is important and in case of collaboration.
Change default settings globally for all R sessions
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.
Set stargazer default output type to identify output type automatically
stargazer use type="latex" by default, which will throw an error when you render your document to HTML. But if you specify stargazer(..., type = "html"), it will have problems when you render your document to PDF. So it is better to set the output type automatically based on the output format of the document.
Set type = ifelse(knitr::is_html_output(), "html", "latex") by default.
# Set stargazer to automatically detect output type (html or latex)
# based on the output format of the document
setHook(
packageEvent("stargazer", "attach"),
function(...) {
assign(
"stargazer",
function(...,
type = if (knitr::is_html_output()) "html" else "latex") {
stargazer::stargazer(..., type = type)
},
envir = .GlobalEnv
)
}
)An easy workaround is to set type="text", which will work for both HTML and PDF outputs, but the table will not be as pretty as the HTML or LaTeX output. text output is a simple ASCII table (monospaced) that is suitable for console output. Good readability but moderate aesthetics. type="text" is a good option for quick previews, but NOT suitable for publication.
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
clangfrom the terminal