I am preparing a tutorial for a course and I want to change the colour of the error to be red. I am using BookDown and gitbook as my output format. But I found that the option class.output
is not working. I want to add a class to the output for the error message I get. How can I do that? You can use this as an example:
我正在准备课程的教程,我想将错误的颜色更改为红色。我使用BookDown和gitbook作为输出格式。但是我发现选项class.output不起作用。我想在输出中为我得到的错误消息添加一个类。我怎样才能做到这一点?您可以将此作为示例:
---
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.
如果没有错误,这可以工作。
1 个解决方案
#1
6
class.output
is not applied to errors (see here).
Following this answer, I suggest you to use an error hook:
class.output不适用于错误(请参阅此处)。根据这个答案,我建议你使用错误钩子:
```{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 a "new" class.error
option in your chunk.
现在,您可以在块中使用“new”class.error选项。
```{r, error = TRUE, class.error="red"}
rnorm(-10)
```
Feel free to open a feature request here.
欢迎在这里打开功能请求。
#1
6
class.output
is not applied to errors (see here).
Following this answer, I suggest you to use an error hook:
class.output不适用于错误(请参阅此处)。根据这个答案,我建议你使用错误钩子:
```{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 a "new" class.error
option in your chunk.
现在,您可以在块中使用“new”class.error选项。
```{r, error = TRUE, class.error="red"}
rnorm(-10)
```
Feel free to open a feature request here.
欢迎在这里打开功能请求。