在Rscript.exe中执行表达式

时间:2013-06-05 07:25:37

标签: r batch-file rscript

我想把一些写东西的表达式直接写入Rscript.exe的调用( > file中指定Rscript [options] [-e expression] file [args]因此没有运行的显式R脚本)。

除了未创建所需文件的事实外,一切似乎都有效。我做错了什么?

# 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')")

1 个答案:

答案 0 :(得分:4)

Rscript使用空格作为分隔符解析其命令行。如果您的R字符串包含嵌入空格,则需要将其包含在引号内以确保它作为完整单元发送到R解析器。

除非您特别需要shell的功能,否则您也无需使用cmd.exe

system("Rscript.exe -e \"write(Sys.time(), file='c:/temp/systime.txt')\"")