【问题标题】:running r scripts or commands with interpretor in unix for unix-layman在 unix 中使用解释器为 unix-layman 运行 r 脚本或命令
【发布时间】:2012-05-15 14:26:05
【问题描述】:

我是 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)

如何在 unix shell 中实现这一点,在两种情况下 -

(1) 通过解释器直接在命令行中 (2) 创建脚本并运行脚本。

考虑到我是 unix 的外行,请提供步骤。

【问题讨论】:

  • 也许你应该使用 R for linux?
  • 对不起这个简单的问题,linux和unix R有什么区别?我相信我们可以在 unix 中运行 R
  • 你试过什么? R 应该可以很好地安装在 unix 或 linux 上,您可以通过命令行使用R 访问它。您还可以查看一些可用的优秀 gui(我建议将 RStudio 作为一个很好的起点)。最后,可以轻松地运行脚本。您经常使用R CMD BATCH script.R,但有很多替代方案和选项都有很好的文档记录。
  • 我建议你通过R documentation。在那里,您对installation 有很好的说明,例如scripting.

标签: linux r shell unix


【解决方案1】:

假设您将脚本保存在一个名为 so.R 的简单文本文件中,您可以在 Linux/Unix 下通过在提示符处键入 R 来运行它。一旦进入 R 输入

  source('so.R')

在 R 环境中执行脚本(假设 so.R 文件与您发出此命令时位于同一目录中)。

要从 Linux/Unix 命令行运行脚本,请使用以下命令:

  R CMD BATCH so.R

请注意,当我在 R 中运行脚本时,我得到了要显示的情节,但在 Linux 命令行中它没有显示。我怀疑它会很快显示然后消失,因此您必须查找一个 R 命令以使其在显示绘图后暂停。

【讨论】:

  • 您可能需要考虑实际的脚本前端 Rscript(与 R 一起提供)或我们较旧的 r(来自我们的小包)。不推荐使用R CMD BATCH 以支持 Rscript。如果你有它,r 也很好。
【解决方案2】:

如果您的程序要处理单个数据集,那么 simple-r 可能是解决方案:

http://code.google.com/p/simple-r/

它是专门为简单的统计分析而设计的,作为 Linux 命令行的一部分。例如,如果想绘制一些数据,'r -p data.txt' 就可以了;获得相关系数:'r cor data.txt'就足够了。

【讨论】:

  • 几年前我们实际上占用了 /usr/bin/r 用于我们更通用的小项目。
【解决方案3】:

我从您提出问题的方式猜测您可能通过 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)

由于您的问题是关于unixlinux 而不是R,您也可以尝试http://unix.stackexchange.com。关于 linux 和 unix 之间的区别有很多话要说,但您可能需要知道的是:download Ubuntu,将其刻录到光盘上,然后使用 CD 驱动器中的光盘重新启动计算机。

希望这会有所帮助。

【讨论】:

  • 它是 小写 R: sudo apt-get install r-base 因为所有包名都是小写的。
  • 感谢@DirkEddelbuettel。固定。
【解决方案4】:

以下示例显示了在 shell 脚本中运行 R 代码的两种方法。两个都 示例也将定义函数而不执行它们,如果脚本是 通过 source() 函数加载到交互式 R 会话中。

第一个示例允许您像向任何其他 shell 一样提供参数 脚本,但不会将额外的 R 选项传递给 R(因为 Rscript 提供 "--args" 到 R 作为参数之一)。

第二个示例允许您提供额外的 R 选项,但会生成 (无害)警告消息,除非您将“--args”作为脚本之一 论据。除非您有特殊要求,否则最好避免使用此版本。

prototype-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))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-22
    • 2014-03-29
    • 2012-03-29
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多