我想将一些将内容写入文件的表达式直接放入对 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')")
最佳答案
Rscript 使用空格作为分隔符来解析其命令行。如果您的 R 字符串包含嵌入空格,您需要将其括在引号内以确保它作为一个完整的单元发送到 R 解析器。
除非您特别需要 cmd.exe
的功能,否则您也不需要使用 shell
。
system("Rscript.exe -e \"write(Sys.time(), file='c:/temp/systime.txt')\"")
关于r - 在 Rscript.exe 中执行表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16933893/