【问题标题】:How to get the error message in try() with rmarkdown?如何使用 rmarkdown 在 try() 中获取错误消息?
【发布时间】:2016-04-18 14:01:33
【问题描述】:

当我使用rmarkdown::render将以下代码编译成html时,

```{r, error=T}
setClass('A', slots=c())
try(setClass('A', slots=c()))
```

我发现第二个命令的 html 文件中没有显示错误消息,而是在命令行中打印。有人知道在 html 文件中也打印错误消息吗?谢谢。

【问题讨论】:

    标签: r r-markdown


    【解决方案1】:

    这里有两种打印错误信息的方法

    ```{r, error=T}
    setClass('A', slots=c())
    
    #method 1
    temp <- try(setClass('A', slots=c()))
    message(temp)
    
    #method 2
    try(setClass('A', slots=c()))
    message(geterrmessage())
    ```
    

    ?try 的详细信息部分找到 2 种方法

    try 计算表达式并捕获在执行过程中发生的任何错误 评估。如果发生错误,则将错误消息打印到 除非 options("show.error.messages") 为 false,否则 stderr 连接 或呼叫包括静默 = TRUE。错误消息也被存储 在缓冲区中,它可以由 geterrmessage 检索。 (这应该 不需要,因为发生错误时返回的值包含 错误信息。)

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 2014-12-24
      • 1970-01-01
      • 2013-02-07
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多