Reputation: 139
```{r, error=TRUE, echo=FALSE, message=FALSE, results='hide', warning=FALSE}
abc1 <- data.frame(content(xyz)$test[[1]]$text)
abc2 <- data.frame(content(xyz)$test[[2]]$text)
abc3 <- data.frame(content(xyz)$test[[3]]$text)
abc4 <- data.frame(content(xyz)$test[[4]]$text)
abc5 <- data.frame(content(xyz)$test[[5]]$text)
```
I have mentioned the r chunk from my rmarkdown as above. I'm creating dataframes abc1,abc2,abc3...abc23 inside an r chunk. The problem is that I get a "OUT OF SUBSCRIPTION BOUND ERROR" as "test[[4]]$text" 4th element of test list does not exist and the process of compilation halts and final output is printed in HTML output.
By making error=TRUE, I can avoid the problem of halting of the compilation. The compilation gets completed but the end result is that the ERROR MESSAGES get printed on the final HTML output. Is there a way to avoid the error messages to be shown or deleted from the final HTML document. I have tried using result='hide' but doesn't seem to solve this issue. How should I get a clean HTML output with rmarkdown without error messages?
Here's the error message that gets printed on the final HTML output which I'm trying to get rid of using r chunk options:
## Error in content(xyz)$test[[4]]: subscript out of bounds
Upvotes: 2
Views: 176
Reputation: 15897
I have not found a chunk option to suppress the error message in the documentation. But you could use the R function try()
to achieve this:
```{r, echo=FALSE}
try({
abc1 <- data.frame(content(xyz)$test[[1]]$text)
abc2 <- data.frame(content(xyz)$test[[2]]$text)
abc3 <- data.frame(content(xyz)$test[[3]]$text)
abc4 <- data.frame(content(xyz)$test[[4]]$text)
abc5 <- data.frame(content(xyz)$test[[5]]$text)
}, silent = TRUE)
```
According to the documentation
try
evaluates an expression and traps any errors that occur during the evaluation.
With the argument silent
, you can control whether error messages should be printed or not. It seems, however, that the HTML file does not contain the error message independently from the value of silent
.
This also means that there is no need for the chunk option error=TRUE
because there no longer is an error. In the present case, also most of the other options are not needed because the chunk does not produce any output, messages, or warnings.
Upvotes: 2