0

标题是不言自明的。这是一个MWE:

summary_table <- iris %>% 
  dplyr::group_by(Species) %>%
  dplyr::summarize(
    `Petal.Length.Spark` = sparkline::spk_chr(`Petal.Length`, 
                                            type = 'line',
                                            chartRangeMin=0,
                                            chartRangeMax=10),
    
  `Petal.Length` = sum(`Petal.Length`)) 

tableHTML::tableHTML(summary_table, rownames=FALSE, 
                     round=2,  
                     escape = FALSE)

我希望添加escape = F会渲染迷你图,但它什么也不渲染。如果你点击“检查元素”,一切看起来都是正确的——例如,第 1 行/第 2 列的标签是:

<td id="tableHTML_column_2"><span id="htmlwidget-9a537f436338a4cd349d" class="sparkline html-widget"></span>
<script type="application/json" data-for="htmlwidget-9a537f436338a4cd349d">{"x":{"values":[4.7,4.5,4.9,4,4.6,4.5,4.7,3.3,4.6,3.9,3.5,4.2,4,4.7,3.6,4.4,4.5,4.1,4.5,3.9,4.8,4,4.9,4.7,4.3,4.4,4.8,5,4.5,3.5,3.8,3.7,3.9,5.1,4.5,4.5,4.7,4.4,4.1,4,4.4,4.6,4,3.3,4.2,4.2,4.2,4.3,3,4.1],"options":{"type":"line","chartRangeMin":0,"chartRangeMax":10,"height":20,"width":60},"width":60,"height":20},"evals":[],"jsHooks":[]}</script></td>

这对我来说是正确的。不完全确定接下来看什么 - 感觉就像缺少与这些行等效的 tableHTML:

  formattable::formattable() %>%
  formattable::as.htmlwidget() %>%
  sparkline::spk_add_deps()

来自sparkline文档sparkline::spk_chr(复制如下,请参见此处:https ://cran.r-project.org/web/packages/sparkline/sparkline.pdf )

library(dplyr)
library(sparkline)
library(formattable)
mtcars %>%
  group_by(cyl) %>%
  summarise(
    hp = spk_chr(
      hp, type="box",
      chartRangeMin=0, chartRangeMax=max(mtcars$hp)
    ),
    mpg = spk_chr(
      mpg, type="box",
      chartRangeMin=0, chartRangeMax=max(mtcars$mpg)
    )
  ) %>%
  formattable() %>%
  formattable::as.htmlwidget() %>%
  spk_add_deps()

任何见解表示赞赏。tableHTML由于此项目中的其他依赖项,我真的更愿意保留它。

4

0 回答 0