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.
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.
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.
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!