Skip to content

Instantly share code, notes, and snippets.

@amcdavid
Last active November 14, 2020 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcdavid/f25fe3861c35b4ae40470ac25a378da4 to your computer and use it in GitHub Desktop.
Save amcdavid/f25fe3861c35b4ae40470ac25a378da4 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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