posit-dev / positron

Positron, a next-generation data science IDE
https://positron.posit.co
Other
2.85k stars 91 forks source link

How to activate renv in notebook #4695

Open petetronic opened 2 months ago

petetronic commented 2 months ago

Logging an issue from a discussion to consider an enhancement for renv management for notebooks in subfolders, and whether the "project" level environment should be set for those notebook sessions automatically if found.

Discussed in https://github.com/posit-dev/positron/discussions/4687

Originally posted by **rbudnar** September 14, 2024 Hi all! My apologies if this is answered elsewhere or if I'm just missing something obvious (I'm a bit of a newcomer to R as well, coming from python). I was testing out using Jupyter notebooks in positron and I was wondering if it is possible to use the local renv for packages. Renvs work great in R scripts without me having to do anything, but in a notebook context it doesn't find my renv-installed packages (where my notebook is under the `src` folder). After messing around for a while, this looks like it worked inside my notebook: ``` setwd("..") # need to go up a directory first, otherwise renv::activate will create a new renv in the current folder renv::activate() .libPaths(renv::paths$library()) ``` My question is - is there a better way to do this? Messing around with paths can get clunky, especially if I need to move things around. Thank you!
juliasilge commented 2 months ago

I chatted with Kevin on this and will share some details, for example on how RStudio handles rendering .Rmd (which also use their own location as the working directory):

The main thing RStudio does here is ensure that the current library paths are forwarded to the child R process invoking rmarkdown::render() via the R_LIBS environment variable, so that the library paths are “inherited” by the child process

We would want to do something that is in spirit similar to: