PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 14, 2022

[FIXED] How to exit knitr on error but still generate the partial output

 November 14, 2022     error-handling, knitr, r     No comments   

Issue

I generate a standardized report using knitr. To document errors easier (the input data is not always what is expected), I need a log file amended with plus nice HTML formatting and figure embedding. A log file lists results until an error occurs, then ends.

The default of knitr, however, is to print the error message into the output and then continue with the next chunks. Alternatively, using knitr::opts_chunk$set(error = FALSE), one can abort the whole process when an error occurs, creating no markdown output at all.

But I want knitr to print until reaching an error, then stop. I can stop early and generate a HTML output using knitr::knit_exit(), but I cannot use it as an error handler. See the two examples below. The first does as expected, but the second one is supposed to output the error message "Test" as the last chunk. How can I achieve this?

MWE 1: knit_exit()

knitr::knit(output = stdout(), text = "
```{r}
1 + 1
```

```{r}
knitr::knit_exit()
```

```{r}
2 + 2
```
")

## ```r
## 1 + 1
## ```
## 
## ```
## ## [1] 2
## ```
## 
## 
## ```r
## knitr::knit_exit()
## ```

MWE 2: No error handling

options(error = knitr::knit_exit); knitr::knit(output = stdout(), text = "
```{r}
1 + 1
```

```{r}
stop('Test')
``` 

This text should not be in the output.

```{r}
2 + 2
```
")



## ```r
## 1 + 1
## ```
## 
## ```
## ## [1] 2
## ```
## 
## 
## ```r
## stop('Test')
## ```
## 
## ```
## Error in eval(expr, envir, enclos): Test
## ```
## 
## This text should not be in the output.
## 
## 
## ```r
## 2 + 2
## ```
## 
```
## [1] 4
```

Solution

That's correct, it does not work to directly use knitr::knit_exit() as an error handler. However, you can override the error output hook with it to achieve your desired outcome:

knitr::knit(
output = stdout(), 
text = "
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)

# override error output hook here
knitr::knit_hooks$set(error = function(x, options) { 
  knitr::knit_exit()
})
```

```{r}
1 + 1
```

```{r}
stop('Test')
``` 

This text should not be in the output.

```{r}
2 + 2
```
")
```

    #> 
    #> 
    #> 
    #> 
    #> ```r
    #> 1 + 1
    #> #> [1] 2
    #> ```
    #> 
    #> 
    #> ```r
    #> stop('Test')
    #> ```

Created on 2022-10-24 with reprex v2.0.2



Answered By - the-mad-statter
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing