4

I'm new to Rscript so please bear with me, I need to execute a R script (written by another developer) from java and for that I'm using Renjin. The R script file and other required files are on classpath i.e. under resources folder.

To achieve this I'm loading Rscript from classpath and it gets loaded successfully, and Rscript tries to load a abc.Rda file internally which is also present in classpath (parallel to R script), the code for same is below

load('abc.Rda')

But when I try to execute script then it throws exception as below:

Could not read from "file:///D:/data/{project-dir}/abc.Rda" because it is a not a file.

Please suggest where I'm making mistake.

coatless
  • 20,011
  • 13
  • 69
  • 84
Akhil
  • 1,184
  • 1
  • 18
  • 42
  • Did you try to give absolute file path to function? – Halil İbrahim Oymacı Jun 29 '16 at 06:38
  • Yup tried that, but the problem is same – Akhil Jun 29 '16 at 07:33
  • I've tried various tests but wasn't able to produce any errors: single, double quotes, capitalized file extension when actual is lower case, etc. `save(d, file='abc.rda')` `load('abc.Rda')` – Bobby Jul 04 '16 at 20:41
  • Could you try `load('abc.Rda', verbose = TRUE)` to see if it gives any more clues? Also, maybe you could look into character encoding: http://stackoverflow.com/questions/34024654/reading-rdata-file-with-different-encoding – Bobby Jul 04 '16 at 22:20
  • What's in the Rda file? Could this be related to or a regression of https://github.com/bedatadriven/renjin/issues/106? (I'm assuming that the R code works without error if run outside of renjin.) – r2evans Jul 05 '16 at 05:42
  • It certainly appears that `{project-dir}` is not being expanded properly. Since R has no notion of a "project" this is likely due to some IDE that is not behaving as expected. RStudio perhaps? – IRTFM Jul 08 '16 at 02:08
  • I've tried executing R script by providing fully classified path name but the error was same. – Akhil Jul 08 '16 at 08:50
  • As far as I know Renjine doesn't implent all R functionalities (at least the version I used one year and half ago) so maybe the R code inside the rda file can't be executed (although the error refers to the file path) – Angelo Immediata Jul 08 '16 at 16:36
  • Please provide a minimally reproducible R script in the open question.. – coatless Jul 09 '16 at 23:20

1 Answers1

1

I'm not sure why using the absolute path didn't work: perhaps the script is incorrectly calling setwd(dir='/bad/{project-dir}') somewhere earlier?

Renjin actually mediates file system operations through Apache Virtual File System (VFS). So if you specifically want a resource on the classpath, you can use the "res:" protocol, for example:

load('res:com/acme/mypackage/abc.Rda')

This should work whether the file is actually on the filesystem or packaged as part of a jar or other archive.

akbertram
  • 1,330
  • 10
  • 16