R node does not properly pick up PATH environment variable

,

I’m using KNIME to process some data and to finally create a report from the processed data. My idea was to use the R package “pander” to collect the chapters and finally export everything with pandoc to HTML or PDF or so. This fails rather unexpectedly, because the R node does not find pandoc. Running R directly, it is found, though.

So I searched around a little bit, and found that the system path environment is clobbered in the KNIME node.

Sys.getenv(“PATH”)
[1] “…\R-4.0.4;…\R-4.0.4\bin\x64\;null”

As you can see, the R home path is added at the head of the list. (I removed the actual path for privacy, but it is correct.) Also: this is on Windows. So backslashes and semicolons are correct as well.

The trailing “null” makes me think, that something goes wrong when starting up the R process. I also didn’t find a place, where to specify an environment variable and possibly override the value.

Is that a bug? Does someone know how to “reset” the environment variable from inside R, so that packages like pander pick up the changed setting?

1 Like

Ah. The markdown messed up the slashes. Of course there are double backslashes there. It was eaten by Markdown and now I cannot edit the post anymore. The right output was like so:

Sys.getenv(“PATH”)
[1] “…\\R-4.0.4;…\\R-4.0.4\\bin\\x64\\;null”

Hi,

I am experiencing the same problem, that R is not able to read the environment on Windows (e.g. PATH etc.). I also want to run pandoc.

Did you find a solution for this? Adding paths manually is of course out of question when sharing the workflow.

Hi all,

I’ve encountered the exact same problem.

I’ve developed an R package that uses git under the hood. It was working perfectly from within Rstudio but not in Knime.

I’ve tried to debug it into Rserve. It was working fine.

After some investigations, I found that R snippets into Knime are not able to read you Windows PATH environment variable.

You can check this by comparing the output of Sys.which(“R”)orSys.which(“Git”)orSys.which(“pandoc”)` into RStudio and Knime. These will return an empty string in KNIME instead of the full path.

To circumvent this problem you can use the following trick : write a new environment variable in your .Renviron file that point to the proper executable file. And then adapt the package you want to use so that it actually makes use of this environment variable.

Example

Let’s say I want my KNIME R snippet be able to work with pander (and so pandoc.exe).

  1. Open Rstudio and execute Sys.which("pandoc")

  2. copy the path returned by R

  3. Open the user .Renviron file and append the following line without the quotation marks (make sure to edit the path according to what was returned by your Sys.which("pandoc") command):
    MY_PANDOC_EXE="C:\\Users\\MYUSERNAME~1\\AppData\\Local\\Pandoc\\pandoc.exe

  4. save and close the .Renviron file and restart Rstudio.

  5. Fork pander package, clone it locally and edit helpers.R file and replace the last path_to_pandoc function by the following lines :

path_to_pandoc = function(){
bin = Sys.getenv("MY_PANDOC_EXE")
bin
  1. From Rstudio, run :
devtools::document()

to re-build the package.

  1. push it to github.com via git push

  2. From Rstudio, re-install the newly forked version

remotes::install_github("yourusername/pander")

  1. Now you can use it properly in KNIME

final words

There could be a better way to achieve this without the need to fork and edit the packages. This is more a quick fix than a true solution but at least it helped me to solve my issue.

Good luck !

2 Likes