Monthly Shaarli
May, 2025
I just realized why my plays keep re-running roles when running a certain role.
It is because I have dutifully replicated the same parameters used in the play for each role in the dependencies
list, which includes the tags
parameter.
So when I call the play with --tags R
, the R
role (which is tagged eponymously) triggers, but also any role in the play which lists R
in its dependencies
, because I tagged the dependencies!
The solution is obvious and I am embarrassed it has taking me so long to realize: do not tag roles in the dependencies
list!
Considered because I was trying to answer the question:
Can we use a different R version per directory/project?
With my R role it has been easy to maintain multiple different R versions on a server. But I have not really been able to pick-and-choose which R version to use with any particular project since there was only one version symlinked globally (/usr/local/bin/R -> ...
).
Could we not set a different version locally, in a particular R project?
- https://github.com/r-lib/rig - this would do it, but does much more than I want, would interfere with my Ansible role
- https://www.monicathieu.com/posts/2024-05-21-multiple-r-versions.html
- https://support.posit.co/hc/en-us/articles/200486138-Changing-R-versions-for-the-RStudio-Desktop-IDE -
export RSTUDIO_WHICH_R=/usr/local/bin/R
- https://github.com/vubiostat/Renv - good, but last commit 13 yrs ago
- https://direnv.net - this probably fits the bill
- https://github.com/hyperupcall/autoenv - similar to
direnv
but smaller in scope - https://github.com/jdx/mise - direnv, make and asdf all in one tool https://news.ycombinator.com/item?id=34583080
- https://github.com/untitaker/quickenv - like
direnv
but less intrusive, more light-weight - https://asdf-vm.com
- https://modules.sourceforge.net - Environment Modules, the OG in this space
apt install modules
- https://github.com/TACC/Lmod - LMod, this is what's used by UPPMAX
asdf
After reading about it a little, I get the feeling it would be more cumbersome than direnv
.
I am not sure I understood correctly, but sounds like the R plugin actually wants to manage the
entire R installation (essentially what rig
does).
- https://github.com/asdf-vm/asdf
- https://asdf-vm.com/guide/introduction.html
- https://github.com/asdf-community/asdf-r - community plugin for R
direnv
I cannot find anyone talking about direnv
with R
on the web. Weird.
- https://direnv.net/#man/direnv.1
- https://direnv.net/#man/direnv-stdlib.1
- https://perrotta.dev/2022/01/direnv-automate-your-environment-variables
- https://rnorth.org/practical-direnv
- https://rnorth.org/more-practical-direnv
- https://pinnsg.com/direnv-take-control-of-your-development-environment
- https://www.digitalocean.com/community/tutorials/how-to-manage-python-with-pyenv-and-direnv
- https://shivamarora.medium.com/a-guide-to-manage-your-environment-variables-in-a-better-way-using-direnv-2c1cd475c8e
- https://platfrastructure.life/post/direnv
- https://github.com/direnv/direnv/issues/73 - some quite interesting example code here
- https://thelinuxcode.com/managing-per-directory-environment-variables-effortlessly-with-direnv
[So what I want] is a "shim" binary that dispatch to the desired binary
https://mise.jdx.dev/dev-tools/shims.htmlThe
.envrc
file is treated like a shell script, where you can also list arbitrary
shell commands that you want to be executed when you enter a project directory.
http://rednafi.com/misc/direnv/#why-envrc-file-and-not-just-a-plain-env-file
My direnv example
Machine in question is Ubuntu 22.04 x64.
$ apt install direnv
$ printf 'if hash direnv >/dev/null 2>&1; then\n eval \"$(direnv hook bash)\"\nfi' >> ~/.bashrc
In the R project directory, I created a subdirectory bin
(for convenience) and created a symlink to the R
binary I wanted, naming it R
. Also added this subdirectory to .gitignore
(and .Rbuildignore
if it's an R package).
In the package directory I created .envrc
containing simply:
PATH_add bin
And enabled with direnv allow
the first time I cd
into the directory.
Now the R
command defined in bin
successfully overrides the system-wide command:
$ ls -l `which R`
lrwxrwxrwx 1 taha taha 18 maj 3 21:47 /media/bay/R/periodicdata/bin/R -> /opt/R/4.5.0/bin/R
Note that this works in the terminal. I am pretty sure we will need to do more to have IDEs such as RStudio Server or Codium Server take heed on a per-directory basis. Will investigate further as time permits.
Support for direnv in RStudio Server
In RStudio Server the built-in terminal behaves like any terminal and respects our direnv
config, but the built-in "R Console" does not, and neither do the other built-in buttons, Knit, etc. So a lot of stuff will use the wrong R version. Unfortunately Posit still to this day lock their "multiple versions of R" functionality behind a "Pro" license, so that cannot help us.
I guess one (really ugly) work-around would be to run multiple RStudio Server instances, each with a different hard-coded R version: export RSTUDIO_WHICH_R=...
. For R package development it's primarily R-release
and R-devel
that matters, so it's not unworkable. And I have been known to run multiple instances anyway (one per LXC container).
Support for direnv in VSCodium or Code Server
You must install the direnv
extension.
In addition, I recommend you reset r.rpath.linux
setting in the workspace to:
{ "settings": { "r.rpath.linux": "R" } }
because otherwise VSCodium will use the default R version during startup (before direnv
takes effect) which causes lots of warnings about missing packages such as jsonlite
or languageserver
.
This works not only in code-server but also in VSCodium on the desktop (assuming you have installed direnv on the system) and when using VSCodium with the Remote SSH extension.
- https://github.com/direnv/direnv/wiki/VSCode recommends https://open-vsx.org/extension/mkhl/direnv
- https://github.com/direnv/direnv-vscode (source code of mkhl/direnv, also on https://codeberg.org/mkhl/vscode-direnv although slightly outdated compared to github)
- https://github.com/cab404/vscode-direnv - another VSCode extension, but explicitly superseded by mkhl version above
- https://mtlynch.io/notes/zig-vscode-nix
- https://github.com/direnv/direnv/issues/231 https://marketplace.visualstudio.com/items?itemName=Rubymaniac.vscode-direnv
- https://discourse.nixos.org/t/using-direnv-nix-shell-and-vscode/17225/10
direnv executes arbitrary shell scripts so this extension requires trusted workspaces
More links and notes
- https://github.com/rstudio/renv/issues/254 - How to deal with different versions of R?
- https://stackoverflow.com/questions/63291613/how-to-set-r-version-in-rproj-file - that's not possible
- https://support.posit.co/hc/en-us/articles/200486138-Changing-R-versions-for-the-RStudio-Desktop-IDE
R versions
At present 2025-05-03:
- R-devel is NA
- R-patched is NA
- R-release is 4.5.0
- R-oldrel is 4.4.3
This is best determined using the rversions::
package!