Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing expressions in Rscript.exe

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')")
like image 935
Rappster Avatar asked Jul 27 '26 17:07

Rappster


1 Answers

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')\"")
like image 172
Hong Ooi Avatar answered Jul 30 '26 08:07

Hong Ooi