Ben2
October 2, 2023, 9:18am
1
Hey all -
I need to load in some variables which can be accessed when calling system()
, for example system("echo $PATH")
- two questions:
How can I add a variable thats available to the system call of RStudio, when launched from Finder (macOS)
Does each call to system()
launch a new shell, or is there one started when the RStudio session starts.
The shell within RStudio is bin/zsh
. The solution needs to be portable as it will be recommended as a guideline for the company.
I have tried adding to ~/.shrc
and ~/.shprofile
to no avail.
thanks,
If you're primarily interested in setting env vars for processes that are launched via system()
calls from R, then .Renviron is probably the best choice.
Using .Renviron to modify PATH
in a way thats portable is a little tricky though because the PATH separator is different on Windows vs macOS/Linux (;
, :
). To modify PATH
in a portable way, you might prefer doing it in .Rprofile, where you can access .Platform$path.sep
. For example, placing something like this in .Rprofile:
local({
set_path <- function(path, action = c("prefix", "suffix", "replace")) {
action <- match.arg(action)
new_entry <- normalizePath(as.character(path), mustWork = FALSE)
path <- strsplit(Sys.getenv("PATH") -> old_path,
.Platform$path.sep, fixed = TRUE)[[1]]
path <- switch(action,
prefix = c(new_entry, path),
suffix = c(path, new_entry),
replace = new_entry)
path <- paste0(path, collapse = .Platform$path.sep)
Sys.setenv(PATH = path)
invisible(old_path)
}
set_path("~/my-bin", action = "prefix")
})
Ben2
October 6, 2023, 11:26am
3
This solves problem, I had no idea system processes loaded the .Renviron
. Thank you for you help.
system
Closed
October 13, 2023, 11:26am
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.