17

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.

Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
Michael
  • 1,537
  • 6
  • 20
  • 42
  • 1
    why dont you add a program or script to your startup on your use from accounts in System Prefrences? Even if you insist on other methods, Launchd would be much better. – AbiusX Mar 15 '11 at 00:05
  • I don't quite understand what you are saying. What is "Launchd"? – Michael Mar 15 '11 at 03:51
  • @AbiusX: No, I don't think that shoehorning R config into MacOS-specific launch daemons is good, it's not portable (unlike ~/.*rc files). It's also too power-user. – smci Feb 19 '18 at 04:58
  • @Michael: not that I second the recommendation for this use (it's overkill), but [Creating Launch Daemons and Agents](https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html) are a MacOS feature you can configure to run whatever jobs you want at startup. – smci Feb 19 '18 at 05:00
  • I used to have my profiles in a file named ``.renviron`` in the ``~`` directory, but after changing laptop it only works with the ``.Rprofile`` name... – PatrickT Apr 02 '18 at 18:53

3 Answers3

20

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thank you so much for your help! I have looked at ?Startup quite a bit, but I will do it more. I don't know what you mean by "And once you grok Renviron versus Renviron.site and dito for Rprofile, you appreciate the consistent behaviour across platforms." though. – Michael Mar 15 '11 at 03:53
  • 1
    I meant to say that maybe you will appreciate the degrees of freedom between user-only and site-wide configuration, which matters if your machine is multi-user as well as sometimes when/how you upgrade R versions. – Dirk Eddelbuettel Mar 15 '11 at 12:32
  • Dirk, so I continue to work my way through the documentation at ?Startup. Much of the language in their is hard to decipher for non-Computer types. For example...."On systems with sub-architectures (mainly Mac OS X and Windows), the files ‘Renviron.site’ and ‘Rprofile.site’ are looked for first in architecture-specific directories, e.g. ‘R_HOME/etc/i386/Renviron.site’. And e.g. ‘.Renviron.i386’ will be used in preference to ‘.Renviron’." I am trying to have R find my Rprofile.site file, and am still unable to do so despite having the instructions for doing so right in front of me. – Michael Mar 15 '11 at 15:18
17

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

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • Thank you for the help! I have saved a file called .Rprofile and placed it in /Users/XXXX but R is still not running it on startup. Even something as simple as >library(MASS) should at least load that library on startup but it doesn't. :( – Michael Mar 15 '11 at 03:50
  • You can see what is actually loaded with >sessionInfo() Your R.app should be in your applications folder (though I'm not sure that matters). The location I described works for me no matter how I launch R. I guess you should go with Dirk's suggestion to study ?Startup which I know is very dense and honestly a little painful. By "grok" he means to get inside R's head and intuitively understand what the scheme is (see Wikipedia for a full definition). – Bryan Hanson Mar 15 '11 at 10:50
  • Oh, and have you searched the R list archives for insight? [link](http://r.789695.n4.nabble.com/) There is also a mailing list specifically for Mac issues. – Bryan Hanson Mar 15 '11 at 10:53
  • Thanks for a link to that archive. Seems like a great resource! Telling people to read ?Startup when they ask how to run functions on startup is a popular response there. – Michael Mar 15 '11 at 15:20
  • 3
    Just a small thing just incase, as I had this issue...if you have any custom functions in there something long the lines of foo <- function(x){ ... ... } make sure there is a line after the last } – h.l.m Jul 17 '12 at 13:58
  • @h.l.m, that little comment of yours just ended 2 hours of head-scratching. Ostensibly an .Rprofile without a newline at the end of the file is completely ignored! – SigmaX Oct 01 '13 at 21:21
2

If you run getwd(), then you will see your R startup directory. On a mac it is typically /User/login_name That is where I leave my .RProfile where I load custom functions and also frequently used packages.

Also see: Useful little functions in R (to put in your .RProfile) and Expert R users, what's in your .Rprofile?…

Community
  • 1
  • 1
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
  • 2
    Is the working directory the same as your home directory? I have tried to save the .RProfile in my working directory and R does not run it on startup. Should my R.app file be in my working directory as well? – Michael Mar 15 '11 at 03:52
  • Fire up R. Then ⌘ + , (or R > Preferences from the menu) and set initial directory to ~ Then close and restart. Does that do it? R.app does not have to be in your working directory. – Maiasaura Mar 15 '11 at 16:50
  • @Michael I'm a bit late to the party, but here is my suggestion. I created a .Rprofile using textedit and saved it in /Users/username, but R would not see it at first. I then realised that, to really create a file without extension in Textedit, one must follow these simple instructions: https://www.quora.com/How-do-I-create-a-text-file-with-no-extension-on-my-Mac I did and R could finally see my .Rprofile. – Marco Plebani Jul 11 '21 at 08:15