3

I have a "copy deployed" installation of R with no R-specific environment variables set.

I want to source a file from within Rprofile.site:

source("how/to/find/this/file/my_settings.R")

Where could I place my my_settings.R file so that it can be found from within Rprofile.site no matter in which path Rprofile.site is installed?

BTW: I want to avoid an absolute path to my_settings.R this would work indeed. I'd prefer to use the same folder as Rprofile.site or a path relative to it to support copy deployment of R.

Edit 1: The problem is that getwd is always different depending on the current folder from which you start R

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • if you type `print(getwd())` just where you plan to write your `source` instruction you should get this location – moodymudskipper Nov 22 '17 at 14:59
  • I have already tried `getwd()` but it seems the path is not stable (at least not within RStudio - it depends on the currently opened project if you restart the R session) – R Yoda Nov 22 '17 at 15:07
  • 1
    you've tried to write this line into your Rprofile file or after the session is opened ? If it's the former it means the project directory is set before Rprofile is read. You could also use `.libPaths()[1]` or `.libPaths()[2]`, assuming you don't plan to mess with those later. On my computer I could put a file in `C:/Program Files/R/R-3.3.1/library` and source it with `source(paste0(.libPaths()[2],"/my_file.R"))` – moodymudskipper Nov 22 '17 at 15:18
  • If have put `getwd` into `Rprofile.site` and started a new R sessions and new RStudio session. In fact `getwd` always returns the current path of command prompt (R session) or the RStudio project root folder. `libPaths()` could be possible workaround but I would prefer to store the sourced file in the same folder als `Rprofile.site` – R Yoda Nov 22 '17 at 15:34
  • `source(paste0(.libPaths()[2],"/etc/my_file.R"))` then ? If your Rprofile isn't there for some reason, you can use whatever relative path you need from libPath and get to your file. – moodymudskipper Nov 22 '17 at 15:40

1 Answers1

3

If you can't use absolute paths and that your working directory is not stable, one way is to use .libPaths or .Library.

By default your Rprofile should be in directory paste0(.Library,"/../etc/") or paste0(.libPaths()[2],"/etc/") so you can put your file there and source it with :

source(paste0(.Library,"/../etc/my_settings.R")) 
source(paste0(.libPaths()[2],"/etc/my_settings.R")) 

As far as I understand the first option is stable (I don't think one can change the value of .Library).

If you use the second option just make sure that if in the future you alter your .libPaths() you do it after sourcing your file.

See ?.libPaths for more info on default folders.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • 1
    Note: This is a good solution for Windows, on Linux the `Rprofile.site` is located in `/etc/R` so searching the file relative to the library path is not possible (but also not necessary if the the absolute path is always the same!). – R Yoda Nov 22 '17 at 17:23