Linux 使用 unix 中的解释器为 unix-layman 运行 r 脚本或命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10602900/
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
running r scripts or commands with interpretor in unix for unix-layman
提问by SHRram
I am layman to unix and sofar I using R in windows. For example I type following in my R session (in R gui).
我是 unix 的外行,到目前为止我在 Windows 中使用 R。例如,我在 R 会话中输入以下内容(在 R gui 中)。
# this is a my funny example script
X <- 1:10
Y <- 21:30
plot(X, Y)
myfun <- function (x){
x1 <- x^0.2
return (x1)
}
myfun(X)
How can I achieve this in unix shell, in two situations -
在两种情况下,我如何在 unix shell 中实现这一点 -
(1) directly in command line via an interpeter (2) creating a script and running script.
(1) 通过 interpeter 直接在命令行中 (2) 创建脚本并运行脚本。
Please provide step considering I am layman to unix.
考虑到我是 unix 的外行,请提供步骤。
采纳答案by Levon
Assuming you save your script in a simple text file with the name so.R
, you can run it under Linux/Unix by typing R
at the prompt. Once in R enter
假设您将脚本保存在一个名为 的简单文本文件中so.R
,您可以在 Linux/Unix 下通过R
在提示符下键入来运行它。一旦在 R 输入
source('so.R')
to execute the script inside the R environment (this assumes the so.R file is in the same directory as you are when you issue this command).
在 R 环境中执行脚本(假设 so.R 文件与您发出此命令时位于同一目录中)。
To run the script from the Linux/Unix command line use the following command:
要从 Linux/Unix 命令行运行脚本,请使用以下命令:
R CMD BATCH so.R
Note that I got the plot to show when I ran the script inside of R, but from the Linux command line it doesn't show. I suspect it gets quickly displayed and then goes away, so there will be a R command that you have to look up to make it pause after it displays the plot.
请注意,当我在 R 中运行脚本时,我得到了要显示的图,但是在 Linux 命令行中它没有显示。我怀疑它会快速显示然后消失,因此您必须查找一个 R 命令才能使其在显示绘图后暂停。
回答by isomorphismes
I'm guessing from the way you worded your question that you maybe SSH'ed into a linux machine? Or that you installed Ubuntu, for example, on your usual laptop/PC.
我从您提出问题的方式中猜测您可能已通过 SSH 连接到 linux 机器?或者,例如,您在常用的笔记本电脑/PC 上安装了 Ubuntu。
Assuming it's the second case: open a terminal and type sudo apt-get install r-base
. Then type R
. Then type
假设是第二种情况:打开一个终端并输入sudo apt-get install r-base
. 然后键入R
。然后输入
X <- 1:10
Y <- 21:30
plot(X, Y)
myfun <- function (x){
x1 <- x^0.2
return (x1)
}
myfun(X)
Since your question is about unix
versus linux
rather than R
, you might also try http://unix.stackexchange.com. There is a lot to be said about the differences between linux and unix, but all you probably need to know is: download Ubuntu, burn it onto a disc, then restart your computer with the disc in your CD drive.
由于您的问题是关于unix
vslinux
而不是R
,您也可以尝试http://unix.stackexchange.com。关于 linux 和 unix 之间的区别有很多要说的,但您可能只需要知道:下载 Ubuntu,将其刻录到光盘上,然后使用 CD 驱动器中的光盘重新启动计算机。
Hope this helps.
希望这可以帮助。
回答by Tom
If your program is going to work on a single dataset, then simple-r might be the solution:
如果您的程序要处理单个数据集,那么 simple-r 可能是解决方案:
http://code.google.com/p/simple-r/
http://code.google.com/p/simple-r/
It is especially designed for simple statistical analysis as a part of Linux command line. For example, if one wants to plot some data, 'r -p data.txt' will do the job; for getting correlation coefficient: 'r cor data.txt' will suffice.
它专为简单的统计分析而设计,作为 Linux 命令行的一部分。例如,如果想绘制一些数据,'r -p data.txt' 就可以完成这项工作;获取相关系数:'r cor data.txt' 就足够了。
回答by Ian E. Gorman
The examples below show two ways to run R code in a shell script. Both examples will also define functions without executing them, if the scripts are loaded to an interactive R session via the source() function.
下面的示例展示了在 shell 脚本中运行 R 代码的两种方法。如果脚本通过 source() 函数加载到交互式 R 会话,则这两个示例还将定义函数而不执行它们。
The first example allows you to give arguments as you would to any other shell script, but will not pass additional R-options to R (because Rscript gives "--args" to R as one of the arguments).
第一个示例允许您像向任何其他 shell 脚本一样提供参数,但不会将额外的 R 选项传递给 R(因为 Rscript 将“--args”作为参数之一提供给 R)。
The second example allows you to give additional R-options, but generates (harmless) warning messages unless you give "--args" as one of the script arguments. This version is best avoided unless you have special requirements.
第二个示例允许您提供额外的 R 选项,但会生成(无害的)警告消息,除非您将“--args”作为脚本参数之一。除非您有特殊要求,否则最好避免使用此版本。
prototype-Rscript.r
原型-Rscript.r
#!/usr/bin/env Rscript
# Prototype R script for use at command line in Linux, Mac OS X, UNIX
# References:
# Manual "A Introduction to R", available via help.start() from the R Console
# Appendix "B.1 Invoking R from the command line" in "A Inroduction to R",
showArguments <- function(argv) {
print(argv)
0
}
if ( ! interactive() ) {
# set some error return codes
SCRIPT_ERROR <- 10 # see documentation for quit()
SCRIPT_ARG_ERROR <- SCRIPT_ERROR + 1
# Define ARGV as script path concatenated to script arguments
ARGV <- commandArgs(FALSE) # start with all the arguments given to R
scriptPath <- sub("^--file=", "", grep("^--file=", ARGV, value=TRUE)) [[1]]
ARGV <- c(scriptPath, commandArgs(TRUE))
if (length(ARGV) < 2) {
cat(file=stderr(), sep="",
"Usage: ", ARGV[[1]], " [ options ] item ...\n",
" Do something with item\n",
" See script for details\n")
quit(save="no", status=SCRIPT_ARG_ERROR)
}
quit(save="no", status=showArguments(ARGV))
}
prototype-shellscript.r
原型-shellscript.r
#!/usr/bin/env R --slave --vanilla --quiet -f
# Prototype R script for use at command line in Linux, Mac OS X, UNIX
# References:
# Manual "A Introduction to R", available via help.start() from the R Console
# Appendix "B.1 Invoking R from the command line" in "A Inroduction to R",
showArguments <- function(argv) {
print(argv)
0
}
if ( ! interactive() ) {
# set some error return codes
SCRIPT_ERROR <- 10 # see documentation for quit()
SCRIPT_ARG_ERROR <- SCRIPT_ERROR + 1
# Define ARGV as the arguments given to this script (after argument “-f”)
ARGV <- commandArgs(FALSE) # start with all the arguments given to R
ARGV <- ARGV[(grep("-f", ARGV) [[1]] + 1):length(ARGV)]
if ( any(grepl("--args", ARGV) )) { # remove arguments intended only for R
ARGV <- c(ARGV[[1]], commandArgs(TRUE))
}
if (length(ARGV) < 2) {
cat(file=stderr(), sep="",
"Usage: ", ARGV[[1]], " [ R_options ] --args [ options ] item ...\n",
" Do something with item\n",
" See script for details\n")
quit(save="no", status=SCRIPT_ARG_ERROR)
}
quit(save="no", status=showArguments(ARGV))
}