在用于使用 kableExtra 打印的函数中粘贴
Pasting within a function used to print with kableExtra
我使用 kableExtra 生成多个表格,我想使用一个函数而不是重复所有代码。但是以我对R的了解有限,我做不到。
下面是一个简化的示例(太简单了,没有说明为什么我要将所有代码折叠成一个函数)。第一个没有添加任何便利功能的代码。
library(kableExtra)
library(glue)
library(tidyverse)
data <- mtcars[1:10, ] |> select(mpg, cyl, am, carb)
# KableExtra, without added convenience function
kbl(data, booktabs = T, linesep = "", digits = 2,
caption = "Ordinary kbl()") |>
add_header_above(c(" ", "Engine" = 2 , "Other" = 2))
)
尝试做同样的事情,现在有一个功能,不同的调用可以使用不同的标题参数并添加 headers。字幕部分工作正常,这是我正在努力添加的 headers。
# Call kableExtra with a function
print_kable <- function(df, caption, header) {
kbl(data, booktabs = T, linesep = "", digits = 2,
# Caption works fine
caption = caption) |>
# I'm unable to develop code that uses a string argument here
add_header_above(c(header)) # 1 col instead of 5
# add_header_above(c({header})) # Same here
# add_header_above(c({{header}})) # and here
# add_header_above(c("{header}")) # and here
# add_header_above(c("{{header}}")) # and here
# add_header_above(c(glue({header}))) # and here
# add_header_above(c(paste(header))) # and here
}
Kable 应使用以下代码打印
print_kable(mtcars, caption = "kbl() called with a function",
header = ' " ", "Engine" = 2 , "Other" = 2 ')
这是一个相关问题:
将函数 c()
放在函数调用中而不是函数本身中效果很好。这是您要找的吗?
print_kable <- function(df, caption, header) {
kbl(data, booktabs = T, linesep = "", digits = 2,
caption = caption) |>
add_header_above(header)
}
print_kable(mtcars, caption = "kbl() called with a function",
header = c(" ", "Engine" = 2 , "Other" = 2))
我使用 kableExtra 生成多个表格,我想使用一个函数而不是重复所有代码。但是以我对R的了解有限,我做不到。
下面是一个简化的示例(太简单了,没有说明为什么我要将所有代码折叠成一个函数)。第一个没有添加任何便利功能的代码。
library(kableExtra)
library(glue)
library(tidyverse)
data <- mtcars[1:10, ] |> select(mpg, cyl, am, carb)
# KableExtra, without added convenience function
kbl(data, booktabs = T, linesep = "", digits = 2,
caption = "Ordinary kbl()") |>
add_header_above(c(" ", "Engine" = 2 , "Other" = 2))
)
尝试做同样的事情,现在有一个功能,不同的调用可以使用不同的标题参数并添加 headers。字幕部分工作正常,这是我正在努力添加的 headers。
# Call kableExtra with a function
print_kable <- function(df, caption, header) {
kbl(data, booktabs = T, linesep = "", digits = 2,
# Caption works fine
caption = caption) |>
# I'm unable to develop code that uses a string argument here
add_header_above(c(header)) # 1 col instead of 5
# add_header_above(c({header})) # Same here
# add_header_above(c({{header}})) # and here
# add_header_above(c("{header}")) # and here
# add_header_above(c("{{header}}")) # and here
# add_header_above(c(glue({header}))) # and here
# add_header_above(c(paste(header))) # and here
}
Kable 应使用以下代码打印
print_kable(mtcars, caption = "kbl() called with a function",
header = ' " ", "Engine" = 2 , "Other" = 2 ')
这是一个相关问题:
将函数 c()
放在函数调用中而不是函数本身中效果很好。这是您要找的吗?
print_kable <- function(df, caption, header) {
kbl(data, booktabs = T, linesep = "", digits = 2,
caption = caption) |>
add_header_above(header)
}
print_kable(mtcars, caption = "kbl() called with a function",
header = c(" ", "Engine" = 2 , "Other" = 2))