我是外行unix和sofar我在Windows中使用R。 例如,我在我的R会话中input以下内容(在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)
我如何在unix shell中实现这一点,有两种情况 –
(1)通过中间人直接在命令行(2)创build脚本和运行脚本。
请提供一步考虑我是外行unix。
假设您将脚本保存在名为so.R
的简单文本文件中,您可以在Linux / Unix下通过在提示符处键入R
来运行它。 一旦进入R进入
source('so.R')
在R环境中执行脚本(假定so.R文件与发出此命令时位于同一目录中)。
要从Linux / Unix命令行运行脚本,请使用以下命令:
R CMD BATCH so.R
请注意,当我在R中运行脚本时,显示了情节,但是从Linux命令行不显示。 我怀疑它快速显示,然后消失,所以将有一个R命令,你必须查找,使其暂停后显示的情节。
如果您的程序要在单个数据集上工作,那么simple-r可能是解决方案:
http://code.google.com/p/simple-r/
作为Linux命令行的一部分,它特别设计用于简单的统计分析。 例如,如果想要绘制一些数据,'r -p data.txt'将会完成这项工作。 为了得到相关系数:'r cor data.txt'就足够了。
我从你的问题的方式猜测,你可能SSH到Linux机器? 或者,例如,您在平常的笔记本电脑/ PC上安装了Ubuntu。
假设是第二种情况:打开终端并键入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)
由于你的问题是关于unix
与linux
而不是R
,你也可以尝试http://unix.stackexchange.com 。 关于linux和unix之间的区别有很多,但是您可能需要知道的一点是: 下载Ubuntu ,刻录到光盘上,然后用CD驱动器中的光盘重新启动计算机。
希望这可以帮助。
以下示例显示了在shell脚本中运行R代码的两种方法。 如果通过source()函数将脚本加载到交互式R会话中,那么这两个示例都将定义函数而不执行它们。
第一个例子允许你给任何其他shell脚本的参数,但不会传递额外的R选项给R(因为Rscript给“–args”给R作为参数之一)。
第二个例子允许你给出额外的R选项,但是会产生(无害的)警告消息,除非你给“–args”作为脚本参数之一。 除非您有特殊要求,否则最好避免使用此版本。
原型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)) }
原型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)) }