1

So for example, I want to set my global options as such:

options(stringsAsFactors = FALSE)
Sys.setenv(JAVA_HOME="C:/Program Files/Java/jre1.8.0_171")

for every RStudio session.

How can I write my code so that they are run at the beginning of each RStudio session?

  • 1
    see `?Startup` and more examples https://stackoverflow.com/questions/1189759/expert-r-users-whats-in-your-rprofile (although one issue with setting options in the .Rprofile is that your script may not be reproducible if executed by another pc) – user20650 Jan 09 '20 at 17:32

1 Answers1

1

Options

You can add the options script to your .Rprofile.

One of the easiest ways to access this is through the usethis library, specifically:

usethis::edit_r_profile()

The .Rproflie is always run at the start of a new session unless specifically told otherwise.

However, I only give you this with a MAJOR warning - adding code into your .Rprofile will prevent your R code from being reproducible. For this reason, I would strongly recommend you set the options call in a snippet in RStudio instead of using the .Rprofile, allowing for a keyboard shortcut to easily add to any script you run. While perhaps less convenient, I believe it's well worth the trade-off to keep your code fully reproducible. You can find more info on snippets with this support article from RStudio.

Envars

The Sys.setenv call would probably be suited well for using a .Renviron file.

Again, easily added with:

usethis::edit_r_environ()

Here is a nice reference to better explain the full use of .Rprofile and .Renviron files: https://cfss.uchicago.edu/notes/r-startup/

Dave Gruenewald
  • 5,329
  • 1
  • 23
  • 35