bash 在命令行(终端)上使用 R 脚本的最佳方式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/750786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to use R scripts on the command line (terminal)?
提问by blahdiblah
It's very convenient to have R scripts for doing simple plots from the command line. However, running R from bash scripts is not convenient at all. The ideal might be something like
使用 R 脚本从命令行进行简单绘图非常方便。但是,从 bash 脚本运行 R 一点也不方便。理想可能是这样的
#!/path/to/R
...
or
或者
#!/usr/bin/env R
...
but I haven't been able to make either of those work.
但我无法完成其中任何一项工作。
Another option is keeping the scripts purely in R, e.g. script.R
, and invoking it with R --file=script.R
or similar. However, occasionally a script will rely on obscure command line switches at which point part of the code exists outside the script. Example: sneaking things into R from bash via a local .Rprofile, the desired switches are then everything --vanilla
implies except --no-init-file
.
另一种选择是将脚本完全保留在 R 中,例如script.R
,并使用R --file=script.R
或类似的方式调用它。然而,有时脚本会依赖于晦涩的命令行开关,此时部分代码存在于脚本之外。示例:通过本地 .Rprofile 将东西从 bash 潜入 R,所需的开关就是--vanilla
除了--no-init-file
.
Another option is a bash script to store the R flags and be painlessly executable, which then calls the R script. The problem is that this means a single program just got split into two files which now have to be keep in sync, transferred to new machines together, etc.
另一种选择是使用 bash 脚本来存储 R 标志并可以轻松执行,然后调用 R 脚本。问题是这意味着一个程序被拆分成两个文件,现在必须保持同步,一起转移到新机器上,等等。
The option I currently despise least is embedding the R in a bash script:
我目前最不喜欢的选项是将 R 嵌入到 bash 脚本中:
#!/bin/bash
... # usage message to catch bad input without invoking R
... # any bash pre-processing of input
... # etc
R --random-flags <<RSCRIPT
# R code goes here
RSCRIPT
Everything's in a single file. It's executable and easily handles arguments. The problem is that combining bash and R like this pretty much eliminates the possibility of any IDE not failing on one or the other, and makes my heart hurt real bad.
一切都在一个文件中。它是可执行的并且可以轻松处理参数。问题是,像这样将 bash 和 R 结合起来几乎消除了任何 IDE 不会在其中一个或另一个上失败的可能性,这让我的心痛得非常厉害。
Is there some better way I'm missing?
有没有更好的方法我失踪了?
回答by
Content of script.r
:
内容script.r
:
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly = TRUE)
message(sprintf("Hello %s", args[1L]))
The first line is the shebang line. It's best practice to use /usr/bin/env Rscript
instead of hard-coding the path to your R installation. Otherwise you risk your script breaking on other computers.
第一行是shebang行。最佳做法是使用/usr/bin/env Rscript
而不是硬编码 R 安装路径。否则,您的脚本可能会在其他计算机上被破坏。
Next, make it executable (on the command line):
接下来,使其可执行(在命令行上):
chmod +x script.r
Invocation from command line:
从命令行调用:
./script.r world
# Hello world
回答by Jouni K. Sepp?nen
回答by The_Cute_Hedgehog
Miguel Sanchez's response is the way it should be. The other way executing Rscript could be 'env' command to run the system wide RScript.
米格尔桑切斯的回应是它应该的方式。执行 Rscript 的另一种方式可能是“env”命令来运行系统范围的 RScript。
#!/usr/bin/env Rscript
回答by The_Cute_Hedgehog
#!/path/to/R
won't work because R is itself a script, so execve
is unhappy.
#!/path/to/R
不会工作,因为 R 本身就是一个脚本,所以execve
不高兴。
I use R --slave -f script
我用 R --slave -f script
回答by phatrick
If you are interested in parsing command line arguments to an R script try RScript which is bundled with R as of version 2.5.x
如果您有兴趣将命令行参数解析为 R 脚本,请尝试从 2.5.x 版开始与 R 捆绑的 RScript
http://stat.ethz.ch/R-manual/R-patched/library/utils/html/Rscript.html
http://stat.ethz.ch/R-manual/R-patched/library/utils/html/Rscript.html
回答by hernamesbarbara
This works,
这有效,
#!/usr/bin/Rscript
but I don't know what happens if you have more than 1 version of R installed on your machine.
但我不知道如果你的机器上安装了 1 个以上的 R 版本会发生什么。
If you do it like this
如果你这样做
#!/usr/bin/env Rscript
it tells the interpreter to just use whatever R appears first on your path.
它告诉解释器只使用在你的路径上首先出现的任何 R。
回答by John
Just a note to add to this post. Later versions of R
seem to have buried Rscript
somewhat. For R 3.1.2-1 on OSX downloaded Jan 2015 I found Rscript
in
只是添加到这篇文章的注释。后来的版本R
似乎Rscript
有些掩埋了。对于 2015 年 1 月下载的 OSX 上的 R 3.1.2-1,我Rscript
在
/sw/Library/Frameworks/R.framework/Versions/3.1/Resources/bin/Rscript
So, instead of something like #! /sw/bin/Rscript
, I needed to use the following at the top of my script.
因此,#! /sw/bin/Rscript
我需要在脚本顶部使用以下内容,而不是类似的内容。
#! /sw/Library/Frameworks/R.framework/Versions/3.1/Resources/bin/Rscript
The locate Rscript
might be helpful to you.
将locate Rscript
可能对您有所帮助。
回答by Andrew Aylett
If the program you're using to execute your script needs parameters, you can put them at the end of the #! line:
如果您用来执行脚本的程序需要参数,您可以将它们放在 #! 线:
#!/usr/bin/R --random --switches --f
Not knowing R, I can't test properly, but this seems to work:
不知道 R,我无法正确测试,但这似乎有效:
axa@artemis:~$ cat r.test
#!/usr/bin/R -q -f
error
axa@artemis:~$ ./r.test
> #!/usr/bin/R -q -f
> error
Error: object "error" not found
Execution halted
axa@artemis:~$
回答by ramanujan
You might want to use python's rpy2 module. However, the "right" way to do this is with R CMD BATCH. You can modify this to write to STDOUT, but the default is to write to a .Rout file. See example below:
您可能想使用 python 的 rpy2 模块。但是,执行此操作的“正确”方法是使用 R CMD BATCH。您可以修改它以写入 STDOUT,但默认值是写入 .Rout 文件。请参阅下面的示例:
[ramanujan:~]$cat foo.R
print(rnorm(10))
[ramanujan:~]$R CMD BATCH foo.R
[ramanujan:~]$cat foo.Rout
R version 2.7.2 (2008-08-25)
Copyright (C) 2008 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
~/.Rprofile loaded.
Welcome at Fri Apr 17 13:33:17 2009
> print(rnorm(10))
[1] 1.5891276 1.1219071 -0.6110963 0.1579430 -0.3104579 1.0072677 -0.1303165 0.6998849 1.9918643 -1.2390156
>
Goodbye at Fri Apr 17 13:33:17 2009
> proc.time()
user system elapsed
0.614 0.050 0.721
Note: you'll want to try out the --vanilla and other options to remove all the startup cruft.
注意:您需要尝试 --vanilla 和其他选项来删除所有启动程序。
回答by Tom
Try smallR for writing quick R scripts in the command line:
尝试使用 smallR 在命令行中编写快速 R 脚本:
http://code.google.com/p/simple-r/
http://code.google.com/p/simple-r/
(r
command in the directory)
(r
目录中的命令)
Plotting from the command line using smallR would look like this:
使用 smallR 从命令行绘制如下所示:
r -p file.txt