Skip to content

Commit

Permalink
enh: honor R_LIBS_USER environment variable when running R
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Oct 18, 2024
1 parent 23f0aa2 commit d85d509
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.62.4
- enh: honor R_LIBS_USER environment variable when running R
0.62.3
- fix: get_r_version string conversion not necessary
0.62.2
Expand Down
30 changes: 23 additions & 7 deletions dclab/lme4/rsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def get_r_version():
require_r()
cmd = (str(get_r_path()), "--version")
logger.debug(f"Looking for R version with: {' '.join(cmd)}")
r_version = run_command(cmd, stderr=sp.STDOUT)
r_version = run_command(
cmd,
env={"R_LIBS_USER": os.environ.get("R_LIBS_USER", "")},
)
r_version = r_version.split(os.linesep)
if r_version[0].startswith("WARNING"):
r_version = r_version[1]
Expand All @@ -93,7 +96,9 @@ def has_lme4():
require_r()
for pkg in ["lme4", "statmod", "nloptr"]:
res = run_command(
(str(get_r_path()), "-q", "-e", f"system.file(package='{pkg}')"))
(str(get_r_path()), "-q", "-e", f"system.file(package='{pkg}')"),
env={"R_LIBS_USER": os.environ.get("R_LIBS_USER", "")},
)
if not res.split("[1]")[1].count(pkg):
avail = False
break
Expand Down Expand Up @@ -123,13 +128,16 @@ def require_lme4():
The packages are installed to the user data directory
given in :const:`lib_path` from the http://cran.rstudio.org mirror.
"""
install_command = ("install.packages("
"c('statmod','nloptr','lme4'),"
"repos='http://cran.rstudio.org'"
")"
)
require_r()
if not has_lme4():
run_command((
get_r_path(), "-e",
"install.packages(c('statmod','nloptr','lme4'),"
"repos='http://cran.rstudio.org')" # noqa: E131
))
run_command(cmd=(get_r_path(), "-e", install_command),
env={"R_LIBS_USER": os.environ.get("R_LIBS_USER", "")},
)


def require_r():
Expand Down Expand Up @@ -175,6 +183,14 @@ def run_command(cmd, **kwargs):
return tmp.strip()


def set_r_lib_path(r_lib_path):
"""Add given directory to the R_LIBS_USER environment variable"""
paths = os.environ.get("R_LIBS_USER", "").split(os.pathsep)
paths = [p for p in paths if p]
paths.append(r_lib_path.strip())
os.environ["R_LIBS_USER"] = os.pathsep.join(paths)


def set_r_path(r_path):
"""Set the path of the R executable/binary"""
tmp = run_command((r_path, "RHOME"))
Expand Down

0 comments on commit d85d509

Please sign in to comment.