gpt4 book ai didi

r - 在 kableExtra 表中结合迷你图

转载 作者:行者123 更新时间:2023-12-04 15:49:57 26 4
gpt4 key购买 nike

我正在尝试创建一个带有副标题和迷你图的表格。

我可以使用 kableExtra 包创建带有副标题的表格。我能够使用 formattablesparkline 包创建一个带有迷你图的表格。

但是,我无法将两者结合起来。有办法吗?我看到了this ,但对我没有帮助。

library(sparkline)
library(tidyverse)
library(formattable)
library(kableExtra)

df <- data.frame(stringsAsFactors=FALSE,
V1 = c("country", "A", "B", "C"),
V2 = c(2000L, 100L, 600L, 50L),
V3 = c(2001L, 200L, 500L, 60L),
V4 = c(2002L, 300L, 400L, 70L),
V5 = c(2003L, 400L, 300L, 80L),
V6 = c(2004L, 500L, 200L, 90L),
V7 = c(2005L, 600L, 100L, 100L)
)

df.names <- df[1,]
names(df) <- df.names
df <- df[-1,]

graph <- df %>%
group_by(country) %>%
gather(key=year, value=value, -country) %>%
summarise(graph=spk_chr(
value,
chartRangeMin = 0,
type="line"))

df2 <- left_join(df, graph, by=c("country"))


df2 %>%
formattable::formattable(align=c("l")) %>%
as.htmlwidget() %>%
spk_add_deps()

enter image description here

df2 %>%
kable("html", caption="Title", escape=T) %>%
kable_styling("striped", full_width = F) %>%
group_rows("group1", 1, 2) %>%
group_rows("group2", 3,3)

enter image description here

df2 %>%
kable("html", caption="Title", escape=T) %>%
kable_styling("striped", full_width = F) %>%
group_rows("group1", 1, 2) %>%
group_rows("group2", 3,3) %>%
formattable::formattable(align=c("l")) %>%
as.htmlwidget() %>%
spk_add_deps()

产生错误:

Error in create_obj(x, "formattable", list(formatter = formatter, format = list(...),  : 
argument "formatter" is missing, with no default

请注意,我尝试了 kable("html", caption="Title", escape=TRUE) 和 escape=FALSE

最佳答案

我遇到了同样的问题。这为我修好了:

library(sparkline)
library(tidyverse)
library(formattable)
library(kableExtra)
library(shiny)
library(htmltools)

df <- data.frame(stringsAsFactors=FALSE,
V1 = c("country", "A", "B", "C"),
V2 = c(2000L, 100L, 600L, 50L),
V3 = c(2001L, 200L, 500L, 60L),
V4 = c(2002L, 300L, 400L, 70L),
V5 = c(2003L, 400L, 300L, 80L),
V6 = c(2004L, 500L, 200L, 90L),
V7 = c(2005L, 600L, 100L, 100L)
)

df.names <- df[1,]
names(df) <- df.names
df <- df[-1,]

graph <- df %>%
group_by(country) %>%
gather(key=year, value=value, -country) %>%
summarise(graph=spk_chr(
value,
chartRangeMin = 0,
type="line"))

df2 <- left_join(df, graph, by=c("country"))

df2 %>%
formattable::format_table(
x = .,
formatters = list(
align=c("l")
)
) %>%
kable_styling("striped", full_width = F) %>%
group_rows("group1", 1, 2) %>%
group_rows("group2", 3,3) %>%
htmltools::HTML() %>%
shiny::div() %>%
sparkline::spk_add_deps()

解决方案是 formattable::format_table() 返回一个 knitr_kable 对象。

关于r - 在 kableExtra 表中结合迷你图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54244718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com