knitr 在渲染 html 时将 (1) 更改为 <ol>?
knitr changes (1) to <ol> when rendering html?
.Rmd 文件的以下内容:
---
title: "Untitled"
output:
html_document: default
---
```{r cars}
mtcars$am <- sprintf("(%s)", as.character(mtcars$am))
knitr::kable(mtcars, format = "html")
```
将在 am
列中显示有序列表 <ol><li></li></ol>
,而不是在呈现为 html 后括号中的数字(与 sprintf
一起生成)。
这是故意的吗?我怎样才能解决这个问题并让括号中的数字显示在 html 输出中?
knitr::kable
的输出似乎没问题,显示:
<td style="text-align:left;"> (1) </td>
详情:
- 使用 knitr 1.20
- RStudio 服务器 1.1.453
- 请注意,删除
format = "html"
并不能解决问题,因为在现实生活中我想使用 css 进行高级格式化,例如基于生成表的类
基于 Michael Harper 接受的答案的快速解决方案可能是这样的方法:
replacechars <- function(x) UseMethod("replacechars")
replacechars.default <- function(x) x
replacechars.character <- function(x) {
x <- gsub("(", "(", x, fixed = TRUE)
x <- gsub(")", ")", x, fixed = TRUE)
x
}
replacechars.factor <- function(x) {
levels(x) <- replacechars(levels(x))
x
}
replacechars.data.frame <- function(x) {
dfnames <- names(x)
x <- data.frame(lapply(x, replacechars), stringsAsFactors = FALSE)
names(x) <- dfnames
x
}
使用示例:
mtcars <- datasets::mtcars
# Create a character with issues
mtcars$am <- sprintf("(%s)", as.character(mtcars$am))
# Create a factor with issues
mtcars$hp <- as.factor(mtcars$hp)
levels(mtcars$hp) <- sprintf("(%s)", levels(mtcars$hp))
replacechars(mtcars)
如果您不想删除 format="html"
参数,您可以尝试使用 HTML 字符实体作为括号(&lpar
和 &rpar
)和然后添加参数 escape = FALSE
:
```{r cars}
mtcars$am <- sprintf("(%s)", as.character(mtcars$am))
knitr::kable(mtcars, format = "html", escape = FALSE)
```
仍然不能完全确定是什么导致了错误。 knitr.
似乎对括号的特定组合进行了奇怪的处理
另一种解决方案是转义括号,例如,
mtcars$am <- sprintf("\(%s)", as.character(mtcars$am))
那你就不需要escape = FALSE
。
参见 Pandoc 手册中的 https://pandoc.org/MANUAL.html#backslash-escapes。
.Rmd 文件的以下内容:
---
title: "Untitled"
output:
html_document: default
---
```{r cars}
mtcars$am <- sprintf("(%s)", as.character(mtcars$am))
knitr::kable(mtcars, format = "html")
```
将在 am
列中显示有序列表 <ol><li></li></ol>
,而不是在呈现为 html 后括号中的数字(与 sprintf
一起生成)。
这是故意的吗?我怎样才能解决这个问题并让括号中的数字显示在 html 输出中?
knitr::kable
的输出似乎没问题,显示:
<td style="text-align:left;"> (1) </td>
详情:
- 使用 knitr 1.20
- RStudio 服务器 1.1.453
- 请注意,删除
format = "html"
并不能解决问题,因为在现实生活中我想使用 css 进行高级格式化,例如基于生成表的类
基于 Michael Harper 接受的答案的快速解决方案可能是这样的方法:
replacechars <- function(x) UseMethod("replacechars")
replacechars.default <- function(x) x
replacechars.character <- function(x) {
x <- gsub("(", "(", x, fixed = TRUE)
x <- gsub(")", ")", x, fixed = TRUE)
x
}
replacechars.factor <- function(x) {
levels(x) <- replacechars(levels(x))
x
}
replacechars.data.frame <- function(x) {
dfnames <- names(x)
x <- data.frame(lapply(x, replacechars), stringsAsFactors = FALSE)
names(x) <- dfnames
x
}
使用示例:
mtcars <- datasets::mtcars
# Create a character with issues
mtcars$am <- sprintf("(%s)", as.character(mtcars$am))
# Create a factor with issues
mtcars$hp <- as.factor(mtcars$hp)
levels(mtcars$hp) <- sprintf("(%s)", levels(mtcars$hp))
replacechars(mtcars)
如果您不想删除 format="html"
参数,您可以尝试使用 HTML 字符实体作为括号(&lpar
和 &rpar
)和然后添加参数 escape = FALSE
:
```{r cars}
mtcars$am <- sprintf("(%s)", as.character(mtcars$am))
knitr::kable(mtcars, format = "html", escape = FALSE)
```
仍然不能完全确定是什么导致了错误。 knitr.
似乎对括号的特定组合进行了奇怪的处理另一种解决方案是转义括号,例如,
mtcars$am <- sprintf("\(%s)", as.character(mtcars$am))
那你就不需要escape = FALSE
。
参见 Pandoc 手册中的 https://pandoc.org/MANUAL.html#backslash-escapes。