【问题标题】:How to do specific table in bookdown with kableExtra如何使用 kableExtra 在 bookdown 中执行特定表
【发布时间】:2021-01-20 20:51:33
【问题描述】:

我尝试在 RMarkown 中将 kableExtra 用于文本表,如下例所示:

text_tbl <- data.frame(
  Items = c("Item 1", "Item 2", "Item 3"),
  Features = c(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. ",
    "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. ", 
    "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. "
  )
)
kbl(text_tbl, booktabs = T) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T, color = "red") %>%
  column_spec(2, width = "30em")

但是我想通过在一个列中添加运算符和在另一列中添加一些解释的示例来更改表格的文本,完全是这个站点:https://www.tutorialspoint.com/r/r_operators.htm

我生成了以下代码,有一些变化,但没有奏效。

text_tbl <- data.frame(
  Items = c(" <- ", " %in% "),
  Features = c(
    " Here it's an example: $x <- 3$",
    " Here it's an example: $c(1, 1, 1, 2, 5, 8, 10) %in% 1$ "
  )
)
kbl(text_tbl, booktabs = T) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T, color = "red") %>%
  column_spec(2, width = "30em")

我想更具体一点:

1 - 如何在数据框中添加运算符,以便在 kable() 创建的表中使用这些运算符? 2 - 如何在由 kable() 创建的表中添加将用完的数据框中的代码?

这两个问题我都会在 rmarkdown 中执行。

谢谢

【问题讨论】:

    标签: r r-markdown bookdown kable kableextra


    【解决方案1】:

    也许这可以帮助你:

    ---
    title: "Untitled"
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(kableExtra)
    ```
    
    ```{r}
    text_tbl <- data.frame(
      Items = c(" $<-$ ", " $$\\%$$in$$\\%$$ "),
      Features = c(
        " Here it's an example: $x <- 3$",
        " Here it's an example: $c(1, 1, 1, 2, 5, 8, 10) $$\\%$$in$$\\%$$ 1$ "
      )
    )
    kbl(text_tbl, booktabs = T, escape = F) %>%
      kable_styling(full_width = F) %>%
      column_spec(1, bold = T, color = "red") %>%
      column_spec(2, width = "30em")
    ```
    

    1 - escape = F 已添加到函数 kbl

    2 - 要插入 %,您需要:$$\\%$$

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2018-10-01
      • 2018-02-10
      • 2020-05-27
      相关资源
      最近更新 更多