R:使用来自DataFrame列中的信息作为KableExtra中的标题

发布于 2025-01-27 14:15:01 字数 265 浏览 4 评论 0原文

使用gt(),您还可以使标题还包括来自数据框架的信息:

table_name <- paste(data$`column_data`[1],paste("Sample Text"))
  table <- gt(data,
              rownames_to_stub = TRUE) %>%
    
    tab_header(title = table_name)

有没有办法使KableExtra做到这一点?

Using gt() you can make the title include information from the data frame in addition to text:

table_name <- paste(data

Using gt() you can make the title include information from the data frame in addition to text:

column_data`[1],paste("Sample Text")) table <- gt(data, rownames_to_stub = TRUE) %>% tab_header(title = table_name)

Is there a way to make KableExtra do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2025-02-03 14:15:01

是的,通过向标头 add_header_above()函数的参数提供数据框。根据该论点的文档:

另外,可以提供带有两个列的数据框架:第一列应
包含标题名称(字符向量)和第二列
应包含colspan(数字向量)。

a {gt}

mtcars |> 
  head(c(5,4)) |> 
  gt(rownames_to_stub = TRUE) |> 
  tab_header(paste(mtcars$drat[1], paste("Sample Text")))

结果:

”在此处输入图像说明“

a {kableExtra}

mtcars |> 
 head(c(5,4)) |> 
 kable() |> 
 kable_styling(full_width = FALSE) |> 
 add_header_above(
     data.frame(
       text = paste(mtcars$drat[1],"Sample Text"), 
       span = 5))  # The number 5 is used to match the number of columns

结果:

Yes, by supplying a data frame to the header argument of add_header_above() function. According to the documentation of this argument:

Alternatively, a data frame with two columns can be provided: The first column should
contain the header names (character vector) and the second column
should contain the colspan (numeric vector).

A {gt} table

mtcars |> 
  head(c(5,4)) |> 
  gt(rownames_to_stub = TRUE) |> 
  tab_header(paste(mtcars$drat[1], paste("Sample Text")))

The result:

enter image description here

A {kableExtra} table

mtcars |> 
 head(c(5,4)) |> 
 kable() |> 
 kable_styling(full_width = FALSE) |> 
 add_header_above(
     data.frame(
       text = paste(mtcars$drat[1],"Sample Text"), 
       span = 5))  # The number 5 is used to match the number of columns

The result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文