Skip to content

Instantly share code, notes, and snippets.

@hoxo-m
Created December 5, 2019 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoxo-m/efe68fb47cc1def452fd8d69e4132178 to your computer and use it in GitHub Desktop.
Save hoxo-m/efe68fb47cc1def452fd8d69e4132178 to your computer and use it in GitHub Desktop.
Sparkline in DT
library(shiny)
library(DT)
library(sparkline)
library(tidyverse)
ui <- fluidPage(
titlePanel("Sparkline in DT"),
sidebarLayout(NULL,
mainPanel(
dataTableOutput("table_with_sparkline")
)
)
)
server <- function(input, output) {
output$table_with_sparkline <- renderDataTable({
df <- iris %>%
group_by(Species) %>%
summarise(Median = median(Sepal.Length), IQR = IQR(Sepal.Length))
my_sparkline_box <- function(x) {
sparkline(x, type = "box",
chartRangeMin = min(iris$Sepal.Length),
chartRangeMax = max(iris$Sepal.Length))
}
df$Sparkline <- iris$Sepal.Length %>%
split(iris$Species) %>%
map(my_sparkline_box) %>%
map(htmltools::as.tags) %>%
map_chr(as.character)
draw_callback <- htmlwidgets::JS("function(){HTMLWidgets.staticRender()}")
dt_options <- list(fnDrawCallback = draw_callback)
datatable(df, options = dt_options, escape = FALSE) %>%
spk_add_deps()
})
}
shinyApp(ui = ui, server = server)
@hoxo-m
Copy link
Author

hoxo-m commented Dec 5, 2019

library(DT)
library(tidyverse)

df <- iris %>%
  group_by(Species) %>%
  summarise(Median = median(Sepal.Length), IQR = IQR(Sepal.Length))
datatable(df)


library(sparkline)

sparkline(iris$Sepal.Length, type = "box")


df$Sparkline <- iris$Sepal.Length %>%
  split(iris$Species) %>%
  map(~ sparkline(.x, type = "box")) %>%
  map(htmltools::as.tags) %>%
  map_chr(as.character)

datatable(df, escape = FALSE) %>% spk_add_deps()


my_sparkline_box <- function(x) {
  sparkline(x, type = "box", 
            chartRangeMin = min(iris$Sepal.Length), 
            chartRangeMax = max(iris$Sepal.Length))
}

df$Sparkline <- iris$Sepal.Length %>%
  split(iris$Species) %>%
  map(my_sparkline_box) %>%
  map(htmltools::as.tags) %>%
  map_chr(as.character)

datatable(df, escape = FALSE) %>% spk_add_deps()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment