rrprofile

Sourcing in Rprofile.site: How to find the location of the sourced file


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


Solution

  • 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.