向表中添加脚注,并在行名称中使用上标数字 - rmd

问题描述 投票:0回答:1

当我使用

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}$")) 
r r-markdown knitr kable
1个回答
0
投票

这与

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}$")
)
```

© www.soinside.com 2019 - 2024. All rights reserved.