cboettig
cboettig

Reputation: 12677

Reload .Renviron or .Rprofile from an active R session (without restarting R)?

When R starts (with default options), it loads the .Rprofile and .Renviron files in the working directory or user's home directory. In my case these files may be changed during the course of the R session (e.g. as packrat does). I was wondering if there is a way to ask R to simply refresh its .Renviron having to quit and restart R?

For instance, in the case of .Rprofile, it is sufficient to simply source(".Rprofile") to reload the .Rprofile from the working directory. Can something similar be managed for .Renviron?

Upvotes: 22

Views: 9014

Answers (2)

stevec
stevec

Reputation: 52268

Use

readRenviron("~/.Renviron")

Full example

Create a file called .Renviron in the directory ~/ (i.e. your home directory). Note: .Renviron must have exactly that filename (the . means it's a hidden file).

Inside .Renviron, place this and save the file:

NEWVAR=123

Now in your R session, read in the .Renviron file and to show all the environment variables:

# Read in .Renviron file you just created
readRenviron("~/.Renviron")

# View environment variables inside your R session
Sys.getenv()
# NEWVAR         123

To access a specific variable:

Sys.getenv("NEWVAR")
# [1] 123

Upvotes: 1

infominer
infominer

Reputation: 2001

Use readRenviron Reference: http://stat.ethz.ch/R-manual/R-devel/library/base/html/readRenviron.html

Yes I remember getting stuck on Sys.getenv a while ago. and thanks to your question, it reminded me about packrat. Going to use it to distribute some code for a manuscript.

Upvotes: 22

Related Questions