Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Rprofile not picked up by RStudio installed through Nix #421

Open
alan-groot opened this issue Feb 14, 2025 · 13 comments
Open

.Rprofile not picked up by RStudio installed through Nix #421

alan-groot opened this issue Feb 14, 2025 · 13 comments

Comments

@alan-groot
Copy link

Hi,

Thanks for the very interesting package!

I was trying out this package to see how easily it would be able to create development environments with RStudio as the IDE, as described in the docs. However, it seems that .Rprofile is not picked up when starting rstudio from the shell, even though it is picked up when I run $(which R) in the Nix shell. See the screenshot below.

Am I doing something wrong, or should I source the file from within RStudio? See the files default.nix and .Rprofile.

Image
Image

@b-rodrigues
Copy link
Contributor

that is quite weird, is the R session within rstudio starting in the right folder? It looks like it from your screenshot though... can you paste the output of .libPaths() here (from RStudio and from an R session)

@alan-groot
Copy link
Author

From within the shell:

[nix-shell:/tmp/.pRhJ]$ pwd
/tmp/.pRhJ

[nix-shell:/tmp/.pRhJ]$ $(which R)

R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> .libPaths()
[1] "/nix/store/afyixhqzjh6c0fy8619hyjgkj4r6l9w9-r-data.table-1.14.0/library"
[2] "/nix/store/992970dl4h9xzzcjmkqb59k77sy5brda-R-4.1.0/lib/R/library"      
> 

From RStudio:

> .libPaths()
 [1] "/home/agroot/R/x86_64-pc-linux-gnu-library/4.1"                          
 [2] "/nix/store/afyixhqzjh6c0fy8619hyjgkj4r6l9w9-r-data.table-1.14.0/library" 
 [3] "/nix/store/992970dl4h9xzzcjmkqb59k77sy5brda-R-4.1.0/lib/R/library"       
 [4] "/nix/store/gvgxyzha3pf29qh98qhqcm2xlj4bh372-r-boot-1.3-28/library"       
 [5] "/nix/store/6l48x3ymc4y191bf43hsssfv5fjhwjkl-r-class-7.3-19/library"      
 [6] "/nix/store/fxlx7xwz020bpqsf68000f40236l05fx-r-MASS-7.3-54/library"       
 [7] "/nix/store/m0arnbdgmm7xqha1a3dpn7bn69gzhbrj-r-cluster-2.1.2/library"     
 [8] "/nix/store/as32vb3x18daf7qnjbx5ffbpymw0z96p-r-codetools-0.2-18/library"  
 [9] "/nix/store/ngczy6k0h1fr41cjnhmf01r39afrnhdk-r-foreign-0.8-81/library"    
[10] "/nix/store/111fgjn7j3rq2zx50rl3n08j9ci1mivr-r-KernSmooth-2.23-20/library"
[11] "/nix/store/k62v8c2rps9shj56wm2przdxqfvgls99-r-lattice-0.20-44/library"   
[12] "/nix/store/9lsyd2k6bh9965yg00w9jb4cyjynizgq-r-Matrix-1.3-4/library"      
[13] "/nix/store/q172n2w2f3bdzjkidxdrczyllk14lx1x-r-mgcv-1.8-36/library"       
[14] "/nix/store/fhy8vwcqz95vrdn5vp3b39qcvrjbiv46-r-nlme-3.1-152/library"      
[15] "/nix/store/40731svpcvhgzhzngkwfz1shb3spfnsx-r-nnet-7.3-16/library"       
[16] "/nix/store/ab8m6fl0x3kp99za4iq8450a5767ia50-r-rpart-4.1-15/library"      
[17] "/nix/store/a3s8dv5y5am1r7lf3mnq5mqzjhjqxv0n-r-spatial-7.3-14/library"    
[18] "/nix/store/rvfvgqxzb8g20nw08cnfi29bg8cv8rg2-r-survival-3.2-11/library" 

@b-rodrigues
Copy link
Contributor

Yeah that .libPaths() from within RStudio doesn’t look good, it should be like the first one. can you try Sys.getenv("RSTUDIO") from the rstudio session? what does it return?

@alan-groot
Copy link
Author

I agree that the RStudio .libPaths() do not look good. Oddly enough, the value of Sys.getenv("RSTUDIO") is 1:

> Sys.getenv("RSTUDIO")
[1] "1"

@alan-groot
Copy link
Author

A workaround is simply sourcing the .Rprofile myself, but I was hoping it would be picked up automatically.

@b-rodrigues
Copy link
Contributor

it really should be automatic, I’m rebuilding the env and will try to reproduce.

@alan-groot
Copy link
Author

alan-groot commented Feb 14, 2025

Great, thanks! Let me know if I can help any further!

@b-rodrigues
Copy link
Contributor

ok so I was able to reproduce this: I don't use RStudio so I never noticed this, but still I'm surprised I never did in 2 years we've been developing rix! It seems to be related to this bug: NixOS/nixpkgs#85840

I'll try to see how to fix this

@alan-groot
Copy link
Author

Thanks in advance!

@alan-groot
Copy link
Author

alan-groot commented Feb 14, 2025

Just summarizing NixOS/nixpkgs#85840, it seems that the .Rprofile file in the working directory is not picked up because the RStudio wrapper sets Sys.getenv("R_PROFILE_USER").

As mentioned in Section 10.8 Customizing the environment of An Introduction to R, "If the environment variable R_PROFILE_USER is set, the file it points to is used instead of the .Rprofile files." (See also https://cran.r-project.org/web/packages/startup/vignettes/startup-intro.html.)

Indeed, in RStudio I get

> Sys.getenv("R_PROFILE_USER")
[1] "/nix/store/svsgpgghlyc73ywk7cqw2aql01h1vb9z-RStudio-2022.07.1+554-wrapper/fix_libs.R"

while

> Sys.getenv("R_PROFILE_USER")
[1] ""

in the shell.

The file .../fix_libs.R contains the following:

# Autogenerated by wrapper-rstudio.nix from R_LIBS_SITE
.libPaths(c(.libPaths(), "/nix/store/gvgxyzha3pf29qh98qhqcm2xlj4bh372-r-boot-1.3-28/library", "/nix/store/6l48x3ymc4y191bf43hsssfv5fjhwjkl-r-class-7.3-19/library", "/nix/store/fxlx7xwz020bpqsf68000f40236l05fx-r-MASS-7.3-54/library", "/nix/store/m0arnbdgmm7xqha1a3dpn7bn69gzhbrj-r-cluster-2.1.2/library", "/nix/store/as32vb3x18daf7qnjbx5ffbpymw0z96p-r-codetools-0.2-18/library", "/nix/store/ngczy6k0h1fr41cjnhmf01r39afrnhdk-r-foreign-0.8-81/library", "/nix/store/111fgjn7j3rq2zx50rl3n08j9ci1mivr-r-KernSmooth-2.23-20/library", "/nix/store/k62v8c2rps9shj56wm2przdxqfvgls99-r-lattice-0.20-44/library", "/nix/store/9lsyd2k6bh9965yg00w9jb4cyjynizgq-r-Matrix-1.3-4/library", "/nix/store/q172n2w2f3bdzjkidxdrczyllk14lx1x-r-mgcv-1.8-36/library", "/nix/store/fhy8vwcqz95vrdn5vp3b39qcvrjbiv46-r-nlme-3.1-152/library", "/nix/store/40731svpcvhgzhzngkwfz1shb3spfnsx-r-nnet-7.3-16/library", "/nix/store/ab8m6fl0x3kp99za4iq8450a5767ia50-r-rpart-4.1-15/library", "/nix/store/a3s8dv5y5am1r7lf3mnq5mqzjhjqxv0n-r-spatial-7.3-14/library", "/nix/store/rvfvgqxzb8g20nw08cnfi29bg8cv8rg2-r-survival-3.2-11/library", "/nix/store/afyixhqzjh6c0fy8619hyjgkj4r6l9w9-r-data.table-1.14.0/library"))

I hope this helps!

I am not sure if the .Rprofile that is created by rix can be appended to the fix_libs.R file through Nix or any other method.

@b-rodrigues
Copy link
Contributor

I opened a PR to wrap R_PROFILE instead NixOS/nixpkgs#382071
that works but there seems to be something else: the local .Rprofile gets loaded, however, the .libPaths() still shows the system-level library of packages. So I get the install.packages(), remove.packages() and update.packages() in my environment tab (so I know .Rprofile was read), but .libPaths() is still wrong. Sourcing .Rprofile again then sets the correct .libPaths(). It seems that maybe one cannot set .libPaths() when executing .Rprofile?

@b-rodrigues
Copy link
Contributor

b-rodrigues commented Feb 14, 2025

So there might be solution: if that PR gets merged, then it seems that creating an .Renviron in the project folder with this:

R_LIBS_USER=""

and then changing the lines in the .Rprofile to this:

        current_paths <- .libPaths()
        .libPaths(current_paths)

seems to work to at least get rid of the user/ level library... but there's still wrong paths from other packages being loaded.

@philipp-baumann
Copy link
Collaborator

So there might be solution: if that PR gets merged, then it seems that creating an .Renviron in the project folder with this:

R_LIBS_USER=""

and then changing the lines in the .Rprofile to this:

        current_paths <- .libPaths()
        .libPaths(current_paths)

seems to work to at least get rid of the user/ level library... but there's still wrong paths from other packages being loaded.

Interesting approach! the RStudio hickup override mechanism is not new to me, but it's good to have a quick-fix for one part at least. I think RStudio has specific override mechanisms for the defaults, that happen even after loading .Rprofile in project dir. It has to be somewhere in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants