Batch file fails to load R packages correctly - function "ymd" not found etc

I have a workflow which I am running from a Windows batch file.
The workflow has an R Snippet which loads a few packages, however when running in batch mode, the workflow fails as soon as it comes across a function from one of the installed packages.

I have tried the following, with no success:

  • installing the packages from within the snippet, before calling them
  • set all packages to ‘require()’
  • manually installing the packages to the SYSTEM library as well as my personal library
  • restarting RServe from within RStudio
  • explicitly stating which package each function comes from, e.g. jsonlite::fromJSON

The snippet runs perfectly fine within RStudio and within the GUI.

One example is the first package function I call from lubridate::ymd
Batch command prompt shows:
Execute failed: Error in R code: “Error: there is no package called ‘lubridate’”

I am out of ideas, and can’t find any in the existing forum posts
Any other suggestions?

Could you provide a minimal workflow that fails in batch and post the corresponding logfile.

I can’t really, because the R script accesses an API with a private token etc

At it’s very basic, any R script that calls a package fails as soon as I try to use a function from that package.

Log File is here: Google Doc

Error is highlighted in red on page 9

The top part of the R script (where it is failing to load packages / functions) is here:

for (package in c('lubridate', 'base64enc', 'httr', 'jsonlite')) {
    if (!require(package, character.only=T, quietly=T, lib.loc="C:/Users/mark.sworn/Documents/R/win-library/3.5")) {
        install.packages(package, repos='http://cran.us.r-project.org', lib="C:/Users/mark.sworn/Documents/R/win-library/3.5")
        library(package, character.only=T, lib.loc="C:/Users/mark.sworn/Documents/R/win-library/3.5")
    }
}

key  <- knime.in$"Key"

untildt <- lubridate::ymd(strftime(as.POSIXlt(Sys.time(), "UTC"), "%Y-%m-%d")) 
sincedt <- lubridate::ymd(untildt) - days(1)
wkspceID <- knime.in$"wkspcid"
knime.out <-data.frame()

As you can see, I check for and then install each package to a specified library location, and then read the package from the specified library location, literally telling R which folder to use each time.

Hi there!

Have you found a solution for this one? I have checked the log but it didn’t tell me much. At least to me :slight_smile:

I assume you are using -workflowDir option? Can you try simplifying the R snippet in a way that it does not use function from this package nor any other in order to try to narrow down the problem? Is that possible in your workflow?

Br,
Ivan

1 Like

It doesn’t seem to matter which libraries I load - they all fail.

Unfortunately there is no easy way of doing what I want to do in R without loading libraries, and using the libraries in R is an essential feature.

I was using the workflowDir option, should the libraries be installed to the workflow directory in order for it to work? I assumed KNIME would access the default R folder set in preferences when loading in batch mode.

Hi there!

WorkflowDir option is ok and to my knowledge KNIME should access the default R folder set in preferences and also all other preferences set.

Sure using libraries in R is essential I just meant to check if the workflow is preforming fine without these libraries confirming this is the real issue and everything else works as expected.

Br,
Ivan

@msworn

according to the log you don’t have the packages installed.

If you are sure that they are installed and accessible installed.packages() should be able to list them. Maybe it’s an option for you to write the output of installed.packages() to some file and double-check that the required libraries are installed.

1 Like

I thought about a few things. Have you tried to execute the lines before this one just from within the R Snippet. Does this also not recognise the lubridate package?

What is the purpose of days(1) - is there a function to substitute that?

Then your batch seems to start from a path with blanks and brackets in the path name. Have you tried to move it to a simpler path like c:\knime_wf\my_wf without any special characters.

1 Like

I have the exact same problem too!
When i run in batch mode (command line mode) it does not load the R libraries.
Knime developers any help?

@thentangler you might check out these entries

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.