经过七个小时的谷歌搜索和重读一些类似的问题,然后进行了大量的试验和错误,我现在可以放心地寻求一些指导。
为了简化我的实际任务,我创建了一个非常基本的R脚本(名为test_script):
x <- c(1,2,3,4,5)
avg <- mean(x)
write.csv(avg, file = "output.csv")这与预期的一样。
我是python的新手,我只是想弄清楚如何执行R脚本,以便创建相同的.csv文件。
值得注意的结果来自:
subprocess.call(["C:/Program Files/R/R-2.15.2/bin/R", 'C:/Users/matt/Desktop/test_script.R'])这将打开一个带有典型R启动语句的命令行窗口,除非有一条消息显示"ARGUMENT 'C:/Users/matt/Desktop/test_script.R‘__ ignored __“
和:
subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', 'C:/Users/matt/Desktop/test_script.r'])这会闪烁cmd窗口并返回0,但不会创建任何.csv文件。
除此之外,我已经尝试了我在这个网站或其他网站上能找到的每一个建议。我们将非常感谢您的任何见解。提前感谢您的时间和努力。
发布于 2013-01-29 09:12:47
在命令提示符下运行R --help将打印以下内容:
Usage: R [options] [< infile] [> outfile]
or: R CMD command [arguments]
Start R, a system for statistical computation and graphics, with the
specified options, or invoke an R tool via the 'R CMD' interface.
Options:
-h, --help Print short help message and exit
--version Print version info and exit
...
-f FILE, --file=FILE Take input from 'FILE'
-e EXPR Execute 'EXPR' and exit
FILE may contain spaces but not shell metacharacers.
Commands:
BATCH Run R in batch mode
COMPILE Compile files for use with R
...试一试
call(["C:/Program Files/R/R-2.15.2/bin/R", '-f', 'C:/Users/matt/Desktop/test_script.R'])您还可以将其他一些命令行参数传递给R,这些参数可能会有所帮助。运行R --help查看完整列表。
发布于 2017-10-23 18:59:51
这可能太晚了,但希望能对其他人有所帮助:
只需在呼叫列表中添加--vanilla即可。
subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', '--vanilla', 'C:/Users/matt/Desktop/test_script.r'])https://stackoverflow.com/questions/14574006
复制相似问题