当我使用
pack_rows()
时,str_replace(df, "S2", "$^{2}$")
在 asis_output()
中不起作用。您能否提供解决此问题的答案。
我在下面添加了可重现的代码,
df <- data.frame(city=c("NYCHACK IT1 N (%)","LA","CHI S2 N (%)","MIA"),
score=sample(1:100, 4, replace=T))
output <- df %>%
kable("latex", booktabs = T, longtable = TRUE, align = 'l',
caption = "caption", linesep = "") %>%
kable_styling(position = "left", latex_options =
c("scale_down","hold_position"), font_size = 8) %>%
pack_rows(index = c(" " = 1,
"Header 1" = 2,
"Header 2" = 1)) %>%
footnote(number = c("Footnote 1",
"Footnote 2"), threeparttable = TRUE)
asis_output(str_replace(output, "IT1", "$^{1}$"),
str_replace(output, "S2", "$^{2}$"))
这与
pack_rows
无关。为了使您的代码正常工作,您必须执行str_replace(output, "IT1", "$^{1}$") |> str_replace("S2", "$^{2}$")
,即在将第一个脚注符号添加到第二个str_replace
中后传递结果:
---
title: "Untitled"
output: pdf_document
date: "2024-02-24"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
library(knitr)
library(stringr)
```
```{r}
df <- data.frame(
city = c("NYCHACK IT1 N (%)", "LA", "CHI S2 N (%)", "MIA"),
score = sample(1:100, 4, replace = T)
)
```
```{r}
output <- df %>%
kable("latex",
booktabs = T, longtable = TRUE, align = "l",
caption = "caption", linesep = ""
) %>%
kable_styling(
position = "left", latex_options =
c("scale_down", "hold_position"), font_size = 8
) %>%
pack_rows(index = c(
" " = 1,
"Header 1" = 2,
"Header 2" = 1
)) %>%
footnote(number = c(
"Footnote 1",
"Footnote 2"
), threeparttable = TRUE)
asis_output(
str_replace(output, "IT1", "$^{1}$") |>
str_replace("S2", "$^{2}$")
)
```