Knit HTML 代码错误无法输出

Knit HTML fails to output upon code error

我的 R 脚本创建了一系列矩阵和这些矩阵的箱线图。有可能其中一个矩阵为空。执行空矩阵的 boxplot 会出错。那不是问题。问题是,当 运行 在 R Markdown 中使用此代码编织一个 HTML 文件时,此错误会停止执行并导致没有 HTML 文件。

作为一个补丁,我认为 运行 boxplot 仅当矩阵不全是 NA 时。这样可行。但是我想知道我是否可以让 knitr 简单地忽略这个错误,而不是修补我的代码。谢谢。

这是错误信息:

Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs) : 
  need finite 'ylim' values
Calls: <Anonymous> ... boxplot -> boxplot.default -> do.call -> bxp -> plot.window
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
Execution halted

可重现代码:

```{r,echo = FALSE, warning = FALSE, message=FALSE}
knitr::opts_chunk$set(cache=TRUE)

#MatrixPos - not empty
MatrixPos <- structure(list(difFwd1 = 0, difFwd2 = 0.200000000000045, difFwd3 = 0.100000000000136,difFwd4 = 0, difFwd5 = 0.200000000000045), .Names = c("difFwd1","difFwd2", "difFwd3", "difFwd4", "difFwd5"), row.names = "155", class = "data.frame") 

#MatrixNeg - empty
MatrixNeg <- structure(list(difFwd1 = NA_real_, difFwd2 = NA_real_, difFwd3 = NA_real_,difFwd4 = NA_real_, difFwd5 = NA_real_), .Names = c("difFwd1","difFwd2", "difFwd3", "difFwd4", "difFwd5"), row.names = "NA", class = "data.frame") 

boxplot(MatrixPos, notch = TRUE, outline = TRUE)
boxplot(MatrixNeg, notch = TRUE, outline = TRUE)

```
###note must remove the four spaces for the code to work in R-Studio


#Solution attempt:
if(!all(is.na(MatrixNeg))) boxplot(MatrixNeg, notch = TRUE, outline = TRUE)

尝试在区块选项中设置 error=TRUE

```{r,echo = FALSE, warning = FALSE, message=FALSE, error=TRUE}
knitr::opts_chunk$set(cache=TRUE)

#MatrixPos - not empty
MatrixPos <- structure(list(difFwd1 = 0, difFwd2 = 0.200000000000045, difFwd3 = 0.100000000000136,difFwd4 = 0, difFwd5 = 0.200000000000045), .Names = c("difFwd1","difFwd2", "difFwd3", "difFwd4", "difFwd5"), row.names = "155", class = "data.frame") 

#MatrixNeg - empty
MatrixNeg <- structure(list(difFwd1 = NA_real_, difFwd2 = NA_real_, difFwd3 = NA_real_,difFwd4 = NA_real_, difFwd5 = NA_real_), .Names = c("difFwd1","difFwd2", "difFwd3", "difFwd4", "difFwd5"), row.names = "NA", class = "data.frame") 

boxplot(MatrixPos, notch = TRUE, outline = TRUE)
boxplot(MatrixNeg, notch = TRUE, outline = TRUE)

```

给出了这个结果:

来自knitr options:"error: (TRUE; logical) whether to preserve errors (from stop()); by default, the evaluation will not stop even in case of errors!! if we want R to stop on errors, we need to set this option to FALSE"