Hello dear R community,
Is there a possibility to specify the renv in which the r code should be executed when running a quarto render command from the console? according to the documentation this magically workds inside RStudio. That is neat, but it does not cover all my use cases, therefore I have these questions:
is it possible to specifically pass an renv as an argument to quarto render?
alternatively, is it possible to enter a renv from the console? This is the approach I take with python environments, where I'd run pipenv shell first and then quarto render.
I have another quetsion, which might be related, but maybe you tell me it is something separate: The 'render' button in RStudio also does not find the appropriate renv when the file is a chapter of a book. When I add renv::load(here::here()) this only seems to work as far as the first chunk. Any ideas what is happening here?
Activation of renv is expected to be automatic when entering the project. It is based on the .Rprofile project file which will be source as part of R initialization. renv environment will be activated at this time. This means the quarto render run inside the project will start R in this project, which will load the renv environment.
In particular, renv/activate.R ensures that the project library is made active for newly launched R sessions. It is automatically sourced via a call to source("renv/activate.R") , which is inserted into the project .Rprofile when renv::init() or renv::activate() is called. This ensures that any new R processes launched within the project directory will use the project library, and hence are isolated from the regular user library.
So if you make sure that you renv environment is activated for this project, Quarto should use it without needing anything else. It should have been done when you initialize with renv::init(). Can you check you .Rprofile in your quarto project ?
R works a bit differently than Python on this, and especially regarding environment. There is no different R version, just different libarary paths loaded when using a renv project.
So you could make sure that your project will use renv by running Rscript -e 'renv::activate() but usually this is done once, and then the environment will be activated by default each time R is run in that working directory.
Hope it helps clarify. Sorry for the delay on a reply
ah yes indeed. the renv.lock is in the main folder of the RStudio Project, whereas the quarto book is at least one folder down. (I tried copying the renv.lock into the quarto book folder, but that did not help)
Any tipps on how I could make the quarto render aware that it is part of a project whose root folder is somewhere else? Or alternatively do you have another suggestion of how to set something like this up - a main analysis project and a quarto book which needs both the renv, some functions and data from the main project?
This is a mutliple step process. renv activates automatically because there is a project .Rprofile that will trigger the renv project by loading the script in the renv folder. So this is the whole renv infrastructure that should be in Quarto book.
A quarto project is a folder with a _quarto.yml - and R process will be run from this root folder. So that is where .Rprofile and other renv stuff should be if you want to use that.
There is no way to tell Quarto to use another project folder for its R process, but maybe you can trick R and renv to load the project in parent folder.
Idea is :
R working dir when Quarto run R code will be your Quarto project. So .Rprofile will be run there if there is one.
You could try tweak this .Rprofile to load renv knowing that the infrastructure is elsewhere.
Does the book require some content from the analysis ? so from a parent folder ?
I think I would consider myself two organisations:
Have my analysis as a folder inside my Quarto book project, and make that folder so it is ignored by Quarto (so that it won't look for book chapter into it or something else). Then have my book elsewhere in the folder (file at root or inside another folder), and use config to explicit chapters
Or consider Analysis and book as two different project, in the sense that Analysis will always run before rendering the book. I would have outputs from my analysis useful to Quarto book stored in specific database file (.rds file, arrow parquet/feather for data.frame, or other DB storage). Quarto book would only read results in there for what it needs to do. This would allow two renv project - one for Analysis and one for Book.
This type of workflow is closed to what tools like targets offers.
A third option is what I mentioned before : Understand how R works at startup to configure the necessary setup in .Rprofile which will be run for each R process started from your Quarto root project.
thanks for posting your ideas! I guess I will set up a separate project for the book. I tried setting the QUARTO_PROJECT_DIR to the main project's root in a pre-render script, but that somehow did not work.
Some follow-up questions regarding your suggestions:
if I use the targets package, but wouldn't that need everything in one (RStudio) project as well?
If I put the main analysis and the quarto book in separate projects as you suggest and write my data to somewhere, the location of that data then would have to be somewhere where both projects have access, i.e. not a subfolder of either project (which is what I usually do), right?
I don't think targets is specific to RStudio Project - but I maybe be wrong. You should read their docs.
yes it would. You could though output in a subfolder of your data project, and then have a pre-render script copy or symlink content in your other project. You could also read from outside your project folder though (for example using a config variable with the absolute path to data folder) - this would work if your data only need to be read by computation for example, and not be resources to copy in output dir.
If you were to use a state less model, you would have you input in a database-like location, output your data to a database-like location, based on a code source (your data project). Then your book project would have access in input to the output database-like location to render the book from the Book project source.
I believe there are several ways to organize your projects, deal with data and codes. You need to find what works for your in the current constraints of the tools used.
Regarding this problem, I believe this is also a renv features request. renv::activate() can call any project path, but it needs to be call inside a R session, and Quarto does not offer that for now. This would also be a feature from quarto be able to specify some configuration that would do that...
Currently Quarto relies solely on the renv activation from .Rprofile in working directory.
And I don't think renv has a way yet to be able to configure an environment activation from terminal before R is started...