Reputation: 3899
I have a conda environment with an R installation. My issue is that the conda R will run ~/.Rprofile
during startup. This is breaking the expectation that conda environments are self-contained. Specifically, I am loading packages in my ~/.Rprofile
that are not installed in the conda R (I am using require()
, so just warnings). The process works the following way:
The first .Rprofile file found on the R startup search path is processed. The search path is (in order): (i)
Sys.getenv("R_PROFILE_USER")
, (ii)./.Rprofile
, and (iii)~/.Rprofile
. Source:startup
package vignette
Ideally, I would like to alter the third path to a location within the environment directory and somehow do this in the environment yaml during setup, such that I can easily replicate the setup on another device. I realize that this might not work, so a solution that permanently sets R_PROFILE_USER
to an environment-specific location would also be appreciated.
Since I am using R through rpy2
I don't think I can use the --no-init-file
flag.
Upvotes: 3
Views: 476
Reputation: 3899
I wrote a bash script to solve this issue. Run it in the project directory containing the environment.yml
. It prompts for a name of the newly created conda environment, then creates and activates it. Subsequently an empty .Rprofile
is created in the environment directory and R_PROFILE_USER
is set to this location. Finally, the environment is reactivated for the change to take effect. Thus, any time R is run from this environment, the newly created .Rprofile
is used.
It should be noted that an .Rprofile
in R_PROFILE_USER
takes precedence over an .Rprofile
in the project directory. This might lead to confusion if the user wants to create use such a file and is unaware of the setup.
echo What name do you want to give to the conda environment?
read input
conda env create -n $input -f environment.yml
conda activate $input
touch $CONDA_PREFIX/.Rprofile
conda env config vars set R_PROFILE_USER=$CONDA_PREFIX
conda activate $input
Upvotes: 0
Reputation: 21532
Quoting from ?.Rprofile
, which invokes the man page for Startup
,
unless --no-init-file was given, R searches for a user profile, a file of R code. The path of this file can be specified by the R_PROFILE_USER environment variable (and tilde expansion will be performed). If this is unset, a file called ‘.Rprofile’ is searched for in the current directory or in the user's home directory (in that order). The user profile file is sourced into the workspace.
Upvotes: 2