【问题标题】:call to sapply() works in interactive mode, not in batch mode调用 sapply() 在交互模式下工作,而不是在批处理模式下
【发布时间】:2012-06-27 00:52:17
【问题描述】:

我需要以批处理模式执行一些命令(例如,通过 Rscript)。它们以交互模式工作,但不能以批处理模式工作。这是一个最小的示例:sapply(1:3, is, "numeric")。为什么这在交互模式下工作但在批处理模式下返回错误?有没有办法让这样的命令在批处理模式下工作?

更具体地说,我需要编写脚本并以批处理模式运行它们。他们需要调用如下所示的函数(我没有编写也无法编辑):

testfun <- function (...)
{
  args <- list(...)
  if (any(!sapply(args, is, "numeric")))
    stop("All arguments must be numeric.")
  else
    writeLines("All arguments look OK.")
}

我需要将一个列表传递给这个函数。像testfun(list(1, 2, 3)) 这样的命令在交互模式下工作。但在批处理模式下,它会产生错误:Error in match.fun(FUN) : object 'is' not found。我试过debugger() 来解决这个问题,但它没有给我任何见解。我还查看了 r-help、R 常见问题解答、R Inferno,但找不到任何与此问题相关的内容。

【问题讨论】:

    标签: r


    【解决方案1】:

    Rscript 默认不加载方法包,因为它需要很多时间。来自?Rscript详细信息部分:

     ‘--default-packages=list’ where ‘list’ is a comma-separated list
          of package names or ‘NULL’.  Sets the environment variable
          ‘R_DEFAULT_PACKAGES’ which determines the packages loaded on
          startup.  The default for ‘Rscript’ omits ‘methods’ as it
          takes about 60% of the startup time.
    

    您可以使用--default-packages 参数使其加载方法。

    > Rscript -e 'sapply(1:3, is, "numeric")' --default-packages='methods'
    [1] TRUE TRUE TRUE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多