【问题标题】:Executing expressions in Rscript.exe在 Rscript.exe 中执行表达式
【发布时间】:2013-05-31 19:07:42
【问题描述】:

我想将一些将内容直接写入文件的表达式放入对 Rscript.exe 的调用中(没有Rscript [options] [-e expression] file [args] 中指定 file,因此没有明确的 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')")

【问题讨论】:

    标签: r batch-file rscript


    【解决方案1】:

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

    您也不需要使用shell,除非您特别需要cmd.exe 的功能。

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

    【讨论】:

    • 太棒了!非常感谢您提供的信息!
    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多