【问题标题】:R command line best practices: exit, print stdout, print stderr, avoid warningsR 命令行最佳实践:退出、打印标准输出、打印标准错误、避免警告
【发布时间】:2013-12-07 18:25:25
【问题描述】:

目前我通过以下方式运行 R 脚本:

R --slave < <script_fullname> argument1 argument2 ...

我想知道 R 中关于如何退出脚本并发出警告的最佳实践,q() 会这样做吗?

if(!file.exists(argument1)){
q()

}

打印到标准输出

    if(!file.exists(argument1)){
    print('file does not exist')
    q()
}

并打印到标准错误?

此外,每次以这种方式运行 R 脚本时,我都会看到以下警告。阅读标准输出时,我看到:

ARGUMENT 'argument1' __ignored__
ARGUMENT 'argument2' __ignored__

有没有办法避免这样的警告?

【问题讨论】:

  • 您忘记了 if 语句的结尾 ')'
  • @Dason 谢谢,已修复。

标签: r


【解决方案1】:

如果您因为错误而要退出 R 脚本,我建议使用
stop("warning message here") 而不是print()q()

不推荐print(),因为“很难捕获并选择性地忽略这种输出。打印输出不是条件,因此您不能使用任何有用的条件处理工具。” -- 来自Debugging, condition handling, and defensive programming

stderr 在this SO post 中讨论(太长,无法在此处重复所有选项)。

尝试使用--args 来避免警告:
R --slave &lt; &lt;script_fullname&gt; --args argument1 argument2 ...

另见Passing Custom Arguments

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2013-05-01
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2018-12-25
    • 2018-08-10
    相关资源
    最近更新 更多