Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sparkline #299

Closed
IgnatiusPang opened this issue Jun 4, 2019 · 6 comments
Closed

sparkline #299

IgnatiusPang opened this issue Jun 4, 2019 · 6 comments

Comments

@IgnatiusPang
Copy link

Dear Developers,

Just a quick question, would the gt package be compatible with the sparkline package (https://github.com/htmlwidgets/sparkline)? (e.g. show sparklines within tables created by gt.)

Kind Regards,

Igy

@rich-iannone
Copy link
Member

The text_transform() function is currently a cumbersome way of getting any HTML you want in specific cells. So, it’s theoretically possible to get output from the sparkline package into specific cells of a gt table. A step in this direction is #155, which is specific to putting in ggplot plots in a column but, should we get the implementation right, this could be extended to output from the sparkline package.

So, we should consider creating a fmt_ function that wraps sparkline and makes it easy to add output to specified table cells.

@IgnatiusPang
Copy link
Author

Agree that a fmt_ function that wraps sparkline and/or based on ggplot will be useful.

@aetiologicCanada
Copy link

I note this: https://leonawicz.github.io/HtmlWidgetExamples/ex_dt_sparkline.html with interest. It is a datatable approach, but may provide ideas to the gt team.

@jthomasmock
Copy link
Collaborator

jthomasmock commented Sep 18, 2020

Another anecdote for ideas to bother from for gt - here is a reprex example for embedding:

  • plotly
  • sparklines

via the reactable table package.

Code Snippet
library(reactable)
library(tidyverse)
library(plotly)
library(glue)
library(htmltools)
library(sparkline)

car_df <- mtcars %>%
  group_by(cyl) %>% 
  summarize(mpg = list(mpg)) %>% 
  dplyr::mutate(
    Sparkline = NA,
    color = c("red", "blue", "green")
  )

car_df %>% 
  reactable(
    # Define columns
    # hide two columns and create a sparkline from the nested MPG data
    columns = list(
      mpg = colDef(show = FALSE),
      color = colDef(show = FALSE),
      Sparkline = colDef(cell = function(value, index){
        sparkline(car_df$mpg[[index]])
      })
    ),
    
    # Details creates a dropdown area that we can fill with a div
    details = function(index){
      match_cyl <- car_df$cyl[index]
      plots_data <- filter(mtcars, cyl == match_cyl)
      plot_col <- filter(car_df, cyl == match_cyl) %>% pull(color)
      htmltools::div(style = "padding: 30px", {
        p1 <- ggplot(plots_data, aes(x = mpg, y = disp)) +
          geom_point(data = mtcars, color = "grey", alpha = 0.5) +
          geom_point(color = plot_col) +
          theme_minimal() +
          scale_x_continuous(limits = c(0, max(mtcars$mpg) * 1.1)) +
          scale_y_continuous(limits = c(0, max(mtcars$disp) * 1.1)) +
          geom_hline(yintercept = 0) +
          geom_vline(xintercept = 0) +
          labs(title = glue::glue("Plot for {match_cyl} cylinders."),
               x = "MPG", y = "Displacement")
        
        plotly::ggplotly(p1)
      }
      )
    }
  )

The kableExtra package has a different implementation and requires explicitly loading a "blank" sparkline(0) to load the JS libs ahead of time.

kableExtra Example

@novotny1akub
Copy link

@rich-iannone could you please elaborate on how to use text_transform() to get sparkline in gt? I tried to do that, but had no success. Here is a reprex on Stack Overflow. It seems to me that I have managed to get the sparkline code there, but I struggle with getting the sparkline dependencies and to overally get it working.

@rich-iannone rich-iannone added this to the FUTURE milestone Aug 22, 2022
@rich-iannone
Copy link
Member

I think this is now reasonably solved with #1431.

@olivroy olivroy modified the milestones: FUTURE, v0.10.0 Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants