Adding to the PATH variable

I am trying to add the path of a package to the PATH environmental variable.

following is the R command I am using:

Sys.setenv(PATH=$PATH:/path to the package/)

But I am getting the following error:

Error: unexpected '$' in "Sys.setenv(PATH=

Anyone knows how can I add the path of a package to the PATH variable in R using the system command?

There appear to be a few issues. First, $PATH is not valid R code. Second, you are as far as I can tell, just simply putting to unquoted strings next to each other. You can do something like this:

old_path <- Sys.getenv("PATH")

Sys.setenv(PATH = paste(old_path, "path/to/package", sep = ":"))

Also, I am not sure if this is system dependent, but my PATH values are separated by semi-colons and not colons. I only used a colon in my sep = argument because it looked like you were trying to use a colon in your example code. You should verify what is used in your PATH by inspecting your old_path (or Sys.getenv("PATH")) variable once it is defined to determine which you need. You can put whatever it is as the sep= argument in the paste function

6 Likes

Just in case , if one wants to modify PATH temporarily for a piece of code, {withr} :package: can be useful.
See http://withr.r-lib.org/reference/with_path.html

6 Likes