简体   繁体   中英

Adding a sparkline to a flextable in R

I am trying to use sparkline with a flextable I created for a report. Unfortunately, I can't get the function tabwid() to work, to convert this into an HTML widget. It is cited as being part of flextable v0.4.0, but it isn't in v0.5.

I note in the documentation below that the tabwid() function is deprecated: https://www.rdocumentation.org/packages/flextable/versions/0.4.0/topics/tabwid

Is there an equivalent function in v0.5? If not, is there another way to use the sparkline function within a sparktable?

I noticed a conversation regarding this with the developer of flextable in 2016, and the code described there includes the tabwid() function. I can't find an alternative and would be grateful for any guidance provided.

https://github.com/davidgohel/flextable/issues/1

The code cited in the link above is as follows:

#devtools::install_github("davidgohel/oxbase") #devtools::install_github("davidgohel/flextable")

#devtools::install_github("htmlwidgets/sparkline")

library(dplyr) library(oxbase) library(flextable) library(sparkline)

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) ) ) %>% flextable() %>% tabwid() %>% spk_add_deps()

If this isn't possible, has anyone else found a way to add a sparkline into a flextable (eg, through another package?)?

Thanks in advance for any guidance you can provide!

With the dev version on github, you can do the following - these are not interactive graphics:

# remotes::install_github("davidgohel/flextable")
library(data.table)
library(magrittr)
library(flextable)

# data prep ----
z <- as.data.table(iris)
z <- z[, list(
  Sepal.Length = mean(Sepal.Length, na.rm = TRUE),
  z = list(.SD$Sepal.Length)
), by = "Species"]

# flextable ----
ft <- flextable(
  data = z,
  col_keys = c("Species", "Sepal.Length", "box", "density", "line")
) %>%
  compose(j = "box", value = as_paragraph(
    plot_chunk(
      value = z, type = "box",
      border = "white", col = "transparent", 
      width = 1.5, height = .4
    )
  )) %>%
  compose(j = "line", value = as_paragraph(
    plot_chunk(value = z, type = "line", col = "white", 
               width = 1.5, height = .4)
  )) %>%
  compose(j = "density", value = as_paragraph(
    plot_chunk(value = z, type = "dens", col = "white", 
               width = 1.5, height = .4)
  )) %>%
  theme_vader() %>%
  align(j = c("box", "density", "line"), align = "center", part = "all") %>%
  autofit()
ft

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM