fail to knit rmd w error even when include=TRUE, error = TRUE,

Hi,
I want to have a way to knit a .Rmd file even if one of the chunks has an error.

I have an .Rmd that starts with this chunk. When I try to knit it it fails with
Line 7 print(x) : object 'x' not found Calls: ... withCallingHandlers -> withVisible ->
eval -> print Execution halted

---
title: "test_knit_with_error"
output: html_document
---

```{r setup}

# , include=TRUE, cache = F
knitr::opts_chunk$set(
  warning = TRUE, # show warnings
  message = TRUE, # show messages
  include=TRUE,
  error = TRUE, # do not interrupt generation in case of errors,
 
  echo = TRUE  # show R code
)


print(x) 



I also tried the same with {r setup , include=TRUE, cache = F} and got same mistake.

Just in case, here is my session info

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C
[7] LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8 LC_ADDRESS=en_US.UTF-8
[10] LC_TELEPHONE=en_US.UTF-8 LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] mailR_0.4.1

loaded via a namespace (and not attached):
[1] digest_0.6.25 R.methodsS3_1.8.1 magrittr_1.5 evaluate_0.14 stringi_1.5.3 rlang_0.4.7
[7] R.oo_1.24.0 R.utils_2.10.1 rmarkdown_2.4 tools_4.0.2 stringr_1.4.0 tinytex_0.26
[13] xfun_0.18 yaml_2.2.1 rsconnect_0.8.16 compiler_4.0.2 rJava_0.9-13 htmltools_0.5.0
[19] knitr_1.30

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C
[7] LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8 LC_ADDRESS=en_US.UTF-8
[10] LC_TELEPHONE=en_US.UTF-8 LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] mailR_0.4.1

loaded via a namespace (and not attached):
[1] digest_0.6.25 R.methodsS3_1.8.1 magrittr_1.5 evaluate_0.14 stringi_1.5.3 rlang_0.4.7
[7] R.oo_1.24.0 R.utils_2.10.1 rmarkdown_2.4 tools_4.0.2 stringr_1.4.0 tinytex_0.26
[13] xfun_0.18 yaml_2.2.1 rsconnect_0.8.16 compiler_4.0.2 rJava_0.9-13 htmltools_0.5.0
[19] knitr_1.30

change to

if(exists("x")) print(x)

Dear technocrat,

Thank you. But print(x) was put there as an intentional error.

What I am trying to figure out is how to make .Rmd knit and produce the html output (including the error message) even if there is an error in one of the chunks (like trying to print an object that does not exist).

Studiosa

OK, more general than just the call to the print() function‐errors in general. For that wrap expressions in tryCatch() and don't supress warnings in setup.

Ok, I figured it out. I need to put error=TRUE in the header of the chunk (`{r setup, error=TRUE, include=TRUE, cache = F}), not just in the body. This knits fine:

```{r setup, error=TRUE, include=TRUE, cache = F}

library(knitr)


knitr::opts_chunk$set(
  warning = TRUE, # show warnings
  message = TRUE, # show messages
  include=TRUE,
  error = TRUE, # do not interrupt generation in case of errors,
 
  echo = TRUE  # show R code
)
 #  error = FALSE, # interrupt generation in case of errors,

# Make error on purpose to test error = TRUE
# It looks like setting global    error = TRUE, does not work
# Have to set error=TRUE in the chunk for the file to knit with this error  
print(x) 

1 + "a"

## Can also use try() and tryCatch() to "catch errors"" in code



log(100)
try(log(100))
str(try(log(100)))

try(log("100"))
str(try(log("100")) )


sessionInfo()




```
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.