【问题标题】:combining sparkline in kableExtra table在 kableExtra 表中组合迷你图
【发布时间】:2019-06-12 04:07:50
【问题描述】:

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

我可以使用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()

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

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) and escape=FALSE

【问题讨论】:

  • 我在尝试让迷你图与 kableExtra 一起工作时遇到了一些问题 - 如果我尝试 df2 %&gt;% kable("html", caption="Title", escape=T) %&gt;% kable_styling("striped", full_width = F) %&gt;% group_rows("group1", 1, 2) %&gt;% group_rows("group2", 3,3) %&gt;% as.htmlwidget() %&gt;% spk_add_deps(),忽略格式化函数,我会得到 Error in UseMethod("as.htmlwidget") : no applicable method for 'as.htmlwidget' applied to an object of class "c('kableExtra', 'knitr_kable')"

标签: r sparklines kableextra formattable


【解决方案1】:

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

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对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多