gpt4 book ai didi

r - 由于代码错误,Knit HTML 无法输出

转载 作者:行者123 更新时间:2023-12-02 04:25:48 25 4
gpt4 key购买 nike

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

作为补丁,我认为仅当矩阵不全为 NA 时才运行 boxplot。这样可行。但我想知道我是否可以让 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)

最佳答案

尝试在 block 选项中设置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)

```

给出了这个结果: enter image description here

来自knitr options :“error:(TRUE;逻辑)是否保留错误(来自stop());默认情况下,即使出现错误,评估也不会停止!!如果我​​们希望R在错误时停止,我们需要设置此选项为假”

关于r - 由于代码错误,Knit HTML 无法输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36848529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com