Question

I'd like to put some expressions that write stuff to a file directly into a call to Rscript.exe (without specifying file in Rscript [options] [-e expression] file [args] and thus without an explicit R script that is run).

Everything seems to work except the fact that the desired file is not created. What am I doing wrong?

# Works: 
shell("Rscript -e print(Sys.time())")

# Works:
write(Sys.time(), file='c:/temp/systime.txt')

# No error, but no file created:
shell("Rscript -e write(Sys.time(), file='c:/temp/systime.txt')")
Was it helpful?

Solution

Rscript parses its command line using spaces as separators. If your R string contains embedded spaces, you need to wrap it within quotes to make sure it gets sent as a complete unit to the R parser.

You also don't need to use shell unless you specifically need features of cmd.exe.

system("Rscript.exe -e \"write(Sys.time(), file='c:/temp/systime.txt')\"")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top