Chunk option.output parameter does not work with error message

I was preparing a course textbook, and I want to change the color of the error so that it is red. I use BookDown and gitbook as the output format. But I found that the class.output option class.output not work. I want to add a class to the output for the error message I receive. How can i do this? You can use this as an example:

 --- title: "Test Book" author: "therimalaya" site: bookdown::bookdown_site output: bookdown::gitbook --- # Hello World ```{r, error = TRUE, class.output="red"} rnorm(-10) ``` 

This works if there is no error.

+7
r chunks knitr r-markdown bookdown
source share
1 answer

class.output does not apply to errors (see here ).
Following this answer , I suggest you take advantage of the error:

 ```{r error-hook, echo=FALSE} knitr::knit_hooks$set(error = function(x, options) { paste0( "```{", ifelse(is.null(options$class.error), "", paste0(" .", gsub(" ", " .", options$class.error)) ), "}\n", x, "\n```" ) }) ``` 

Now you can use the "new" class.error option in your piece.

 ```{r, error = TRUE, class.error="red"} rnorm(-10) ``` 

Feel free to open a feature request here .

+6
source share

All Articles