Skip to content

Instantly share code, notes, and snippets.

@amcdavid
Last active Nov 14, 2020
Embed
What would you like to do?
Load environment modules (https://modules.readthedocs.io/en/latest/index.html) after R is started
local({
# modify as needed
modules = c("git", "git-lfs", "gcc")
capt = system2("./capture_module_environment.sh", modules, stdout = TRUE)
split = strsplit(capt, '=', fixed = TRUE)
out = lapply(split, function(v){
do.call(Sys.setenv, stats::setNames(list(v[2]), v[1]))
})
message("Added environment variables for ", paste(modules, collapse = ', '))
})
#!/bin/sh
module load $@
echo -n "PATH="
printenv PATH
echo -n "LD_LIBRARY_PATH="
printenv LD_LIBRARY_PATH
# if other variables are set you might need add more echo and printenv statements
@amcdavid

This comment has been minimized.

Copy link
Owner Author

@amcdavid amcdavid commented Nov 13, 2020

rstudio-server generally doesn't inherit the environment of the calling shell. On systems that use environment modules to select among available software, rstudio server won't "see" software that was loaded with the modules. capture_module_environment.sh loads the modules and captures changes to PATH and LD_LIBRARY_PATH which are then manually set on the R process with Sys.setenv(). If done in .Rprofile, then this will have the same effect as if it were done before R was loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment