当编织到 html 时,knitr::kable 没有在字符串数据中正确转义特殊字符 $(到 PDF 是可以的)
knitr::kable does not escape special character $ correctly in the string data when knitring to html (to PDF is okay)
数据中有 $ 符号 knitr::kable 很混乱。显示的数据就好像它们本应处于数学模式。有趣的是,knitr to PDF 并没有显示这种效果。
我尝试用反斜杠转义美元符号(甚至尝试使用最多四个反斜杠),使用块选项“标记”作为结果,在 kable 中触发自动转义,或者使用十六进制-Unicode 而不是字符。没有任何帮助。
非常欢迎任何进一步的想法。
我使用 Rstudio 以 HTML 或 PDF 格式编织。这是我的 *.Rmd:
中的 MWE
---
title: MWE knitr::kable does not escape special character $ correctly in the string data when knitring to html (to PDF is okay)
output:
html_document:
df_print: paged
pdf_document: default
---
```{r UNESCAPED , eval=TRUE , echo=FALSE , tidy=TRUE, message=FALSE , warning=FALSE , results='asis'}
library ("knitr")
library("kableExtra")#not needed but helpful to see the problem
library("tidyverse")#not needed but helpful to see the problem
data <- data.frame(stringVar=c("$","$"))
#escaping $ shows error message:
#data <- data.frame(stringVar=c("$1$","$2$"))
if (knitr::is_html_output())
{
knitr::kable(data, row.names = TRUE, format = "html" , escape = TRUE)%>% #same effect with escape=FALSE
kable_styling( "striped")#same effect without kable_styling
#last cell is rendered in math mode
}else{
knitr::kable(data, format="latex", longtable=T, booktabs = T, linesep = "", row.names = TRUE, escape = TRUE) %>%
kable_styling(latex_options =c("striped"))
#last cell is rendered as expected
}#end of if
```
我可以使用反引号使其工作:
编辑:
@aosmith 是正确的 - 双转义是更好的答案,因为它不会用背景阴影渲染它
---
title: MWE knitr::kable does not escape special character $ correctly in the string data when knitring to html (to PDF is okay)
output:
html_document:
df_print: paged
pdf_document: default
---
```{r UNESCAPED , eval=TRUE , echo=FALSE , tidy=TRUE, message=FALSE , warning=FALSE , results='asis'}
library ("knitr")
library("kableExtra")#not needed but helpful to see the problem
library("tidyverse")#not needed but helpful to see the problem
data <- data.frame(stringVar=c("$","$"))
if (knitr::is_html_output())
{
# data %>% mutate_all( stringr::str_replace_all, pattern=fixed("$"), replacement="`$`") %>%
data %>% mutate_all( stringr::str_replace_all, pattern=fixed("$"), replacement="\$") %>%
knitr::kable( row.names = TRUE, format = "html" , escape = FALSE) %>% #same effect with escape=FALSE
kable_styling( "striped")#same effect without kable_styling
#last cell is rendered in math mode
}else{
knitr::kable(data, format="latex", longtable=T, booktabs = T, linesep = "", row.names = TRUE, escape = TRUE) %>%
kable_styling(latex_options =c("striped"))
#last cell is rendered as expected
}#end of if
```
数据中有 $ 符号 knitr::kable 很混乱。显示的数据就好像它们本应处于数学模式。有趣的是,knitr to PDF 并没有显示这种效果。
我尝试用反斜杠转义美元符号(甚至尝试使用最多四个反斜杠),使用块选项“标记”作为结果,在 kable 中触发自动转义,或者使用十六进制-Unicode 而不是字符。没有任何帮助。 非常欢迎任何进一步的想法。
我使用 Rstudio 以 HTML 或 PDF 格式编织。这是我的 *.Rmd:
中的 MWE---
title: MWE knitr::kable does not escape special character $ correctly in the string data when knitring to html (to PDF is okay)
output:
html_document:
df_print: paged
pdf_document: default
---
```{r UNESCAPED , eval=TRUE , echo=FALSE , tidy=TRUE, message=FALSE , warning=FALSE , results='asis'}
library ("knitr")
library("kableExtra")#not needed but helpful to see the problem
library("tidyverse")#not needed but helpful to see the problem
data <- data.frame(stringVar=c("$","$"))
#escaping $ shows error message:
#data <- data.frame(stringVar=c("$1$","$2$"))
if (knitr::is_html_output())
{
knitr::kable(data, row.names = TRUE, format = "html" , escape = TRUE)%>% #same effect with escape=FALSE
kable_styling( "striped")#same effect without kable_styling
#last cell is rendered in math mode
}else{
knitr::kable(data, format="latex", longtable=T, booktabs = T, linesep = "", row.names = TRUE, escape = TRUE) %>%
kable_styling(latex_options =c("striped"))
#last cell is rendered as expected
}#end of if
```
我可以使用反引号使其工作:
编辑:
@aosmith 是正确的 - 双转义是更好的答案,因为它不会用背景阴影渲染它
---
title: MWE knitr::kable does not escape special character $ correctly in the string data when knitring to html (to PDF is okay)
output:
html_document:
df_print: paged
pdf_document: default
---
```{r UNESCAPED , eval=TRUE , echo=FALSE , tidy=TRUE, message=FALSE , warning=FALSE , results='asis'}
library ("knitr")
library("kableExtra")#not needed but helpful to see the problem
library("tidyverse")#not needed but helpful to see the problem
data <- data.frame(stringVar=c("$","$"))
if (knitr::is_html_output())
{
# data %>% mutate_all( stringr::str_replace_all, pattern=fixed("$"), replacement="`$`") %>%
data %>% mutate_all( stringr::str_replace_all, pattern=fixed("$"), replacement="\$") %>%
knitr::kable( row.names = TRUE, format = "html" , escape = FALSE) %>% #same effect with escape=FALSE
kable_styling( "striped")#same effect without kable_styling
#last cell is rendered in math mode
}else{
knitr::kable(data, format="latex", longtable=T, booktabs = T, linesep = "", row.names = TRUE, escape = TRUE) %>%
kable_styling(latex_options =c("striped"))
#last cell is rendered as expected
}#end of if
```