Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically load settings in R on OSX? How to find R_HOME, configure Rprofile.site, etc?

Tags:

r

startup

I have a Macintosh and I am trying to automatically load packages, homemade functions, and use modified setting every time I start R. I believe this can be done with a file called Rprofile.site, and by creating the functions .First and .Last in that file.

One problem is, I have no idea what my R_HOME directory is, what it is used for, or if it even exists. I found two functions that I thought both gave me its location but I am getting different results.

Here's the first

> Sys.getenv("R_home")
R_home 
    ""

And the second

> R.home()
[1] "/Library/Frameworks/R.framework/Resources"

As far as I can tell that second directory doesn't even exist on my machine. I am currently running R from my applications directory.

like image 488
Michael Avatar asked Mar 14 '11 23:03

Michael


People also ask

Where is Rprofile on Mac?

Typically . Rprofile is located in the users' home directory ( ~/. Rprofile on MacOS/Linux).

Where does R Look for Renviron?

Renviron file is sourced. Typically . Rprofile is located in the users' home directory ( ~/. Rprofile ), however a different location can be configured by setting the R_PROFILE_USER environment variable.

Which file contains settings that R uses by default?

When you start R, it will by default source a . Rprofile file if it exists. This allows you to automatically tweak your R settings to meet your everyday needs.


2 Answers

Over the years I have come to rely on the help(Startup) documentation as the best place to read up on this. There are numerous per-user and per-site configuration file as is customary for rich applications. It may seem like overkill at first but it is a really good system. And once you grok Renviron versus Renviron.site and dito for Rprofile, you appreciate the consistent behaviour across platforms.

like image 108
Dirk Eddelbuettel Avatar answered Oct 05 '22 23:10

Dirk Eddelbuettel


Michael, I too have found this topic to be a bit confusing. I'm on a Mac as well. I created an "Rprofile" file which has all my customizations in it. Here's how mine works (I don't think there is anything special about my set up):

  1. The "Rprofile" goes in /Users/michael
  2. The "Rprofile" has to be composed of commands that R will understand (for instance, you can source it).
  3. The "Rprofile" has to be called .Rprofile The leading period means that the file is hidden from the normal operating system. You have to open a terminal window and do an >ls -la to see it (assuming you cd to that directory, if necessary). Plus you'll see lots of other hidden files. And it probably doesn't exist until you create it, next step.
  4. I use TextEdit to create a file called R.txt and put the commands in there (start simple for testing purposes).
  5. Then, in a terminal window, I type >cp R.txt .Rprofile which copies the visible R.txt to the invisible .Rprofile You can check by doing >ls -la again to see it in the directory listing.
  6. Restart R and see if it worked. For instance, if you put library(ggplot2) in your R.txt, that library should be loaded upon start up. If it doesn't, then a command from that library won't work, like qplot(x = 1:10, y = 1:10). Other people put in commands like cat("My .Rprofile works!\n) which should display during launching.

HTH Bryan

like image 22
Bryan Hanson Avatar answered Oct 05 '22 23:10

Bryan Hanson