Working dirs and reproducible research

When I schedule an R script using the taskschedulerR library, R is executed at C:/windows/system32 which screws up all the relative paths in my R Script. The script I am trying to run looks like this, and I am making use of the here package.



This runs fine in RStudio.

I then use the library taskscheduleR, like so, to create a regular run of my main.R script.

taskscheduler_create(taskname = "my_main_script",
rscript = here::here('main.R'),
schedule = "MINUTE",
starttime = '11:00',
modifier = 30)

But in the log for this, I am given this error ....

Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
Calls: source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'C:/Windows/System32/script1.R': No such file or directory
Execution halted

I understand why this is happening, but am unsure of an elegant way to fix it. I don't want to hardcode the path of the files in the source command, as I am trying to design my code to be portable between different environments.

Thanks for any help you might be able to offer.

It sounds like your proof of concept works, and now you're working on how to wrap it together so that it works elsewhere. This is probably the point where I would make the scripts into a proper R package.

The advantage to your case of using a package is that you can call system.file( c("path", "to","file"), package="packagename") and it will return an absolute path to the file. The path to file is relative to the package root, so you don't have to muck about with the here pacakge or trying to sort out where you are in the filesystem yourself.

For example, a package "foo" with the structure:

├── R
│   ├── list_suites.R
│   ├── lookup_measure.R
│   ├── measure_extractors.R

The absolute path to the measrue_extrators.R file can be retrieved by:

> system.file(c("R","measure_extractors.R"), package="foo")
[1] "/home/janedoe/R/x86_64-pc-linux-gnu-library/lib/spekex/R"

This does require the package is installed in a location that R can find it. In the above case, it's returning the path to where the user janedoes's packages are stored. If task scheduler is using the system R library location, it will have to be ensured that the package gets installed there.


Thanks for the tips @grosscol . Someone on Reddit also suggested that writing a package would be the best way to go for this. I guess I'd better make that leap!

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