Created
December 5, 2019 22:44
-
-
Save hoxo-m/efe68fb47cc1def452fd8d69e4132178 to your computer and use it in GitHub Desktop.
Sparkline in DT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Author
hoxo-m
commented
Dec 5, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment