I´m trying to add sparklines to a simple reactable example but the table returns blank when I try to do so.
I´ve been following this example from the documentation.
This is the sample data I´m using:
PartName <- c("550-1", "550-2", "550-3", "550-4")
Orders <- c(50, 35, 2, 18)
condensed <- data.frame(PartName, Orders)
PartName <- c("550-1", "550-1", "550-1", "550-1", "550-1", "550-2", "550-2", "550-2", "550-2", "550-3", "550-4", "550-4", "550-4")
Size <- c(10, 10, 10, 10, 10, 15, 5, 10, 5, 2, 6, 6, 6)
Date <- as.Date(c("2020/10/01", "2020/12/01", "2021/01/01", "2021/03/01", "2021/04/01", "2020/12/01", "2021/03/01", "2021/04/01", "2021/06/01", "2020/11/01", "2020/10/01", "2020/11/01", "2020/12/01"))
orders <- data.frame(PartName, Size, Date)
This is how it looks the data I´m passing to reactable:
And this is the shiny app:
library(shiny)
library(shinydashboard)
library(sparkline)
library(dplyr)
library(tidyr)
PartName <- c("550-1", "550-2", "550-3", "550-4")
Orders <- c(50, 35, 2, 18)
condensed <- data.frame(PartName, Orders)
PartName <- c("550-1", "550-1", "550-1", "550-1", "550-1", "550-2", "550-2", "550-2", "550-2", "550-3", "550-4", "550-4", "550-4")
Size <- c(10, 10, 10, 10, 10, 15, 5, 10, 5, 2, 6, 6, 6)
Date <- as.Date(c("2020/10/01", "2020/12/01", "2021/01/01", "2021/03/01", "2021/04/01", "2020/12/01", "2021/03/01", "2021/04/01", "2021/06/01", "2020/11/01", "2020/10/01", "2020/11/01", "2020/12/01"))
orders <- data.frame(PartName, Size, Date)
df <- orders %>%
dplyr::mutate(Date = format(Date, "%Y-%m-%d")) %>%
dplyr::group_by(PartName) %>%
tidyr::complete(Date = c('2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01', '2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01','2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01'), fill = list(Size = 0)) %>%
as.data.frame() %>%
dplyr::group_by(PartName) %>%
dplyr::summarize(ORDRS = paste0(Size, collapse = ",")) %>%
dplyr::group_by(PartName) %>%
dplyr::summarise(ORDRS = list(ORDRS))
data <- condensed %>%
left_join(df, by = c("PartName" = "PartName"))
ui <- dashboardPage(
dashboardHeader(title = "Title"),
dashboardSidebar(
sidebarMenu(),
collapsed = TRUE
),
dashboardBody(
fluidRow(
box(
title = "Table with sparklines", status = "primary", solidHeader = TRUE, collapsible = TRUE, width = 12,
reactableOutput("table")
)
)
)
)
server <- function(input, output) {
output$table <- renderReactable({
reactable(data, columns = list(
PartName = colDef(name = PN),
Orders = colDef(name = Orders),
ORDRS = colDef(cell = function(values) {
sparkline(values, type = "bar", chartRangeMin = 0, chartRangeMax = 12)
})
# ORDRS = colDef(cell = function(value, index) {
# sparkline(data$ORDRS[[index]], type = "bar", chartRangeMin = 0, chartRangeMax = 12)
# })
))
})
}
shinyApp(ui = ui, server = server)
If you remove this part:
ORDRS = colDef(cell = function(values) {
sparkline(values, type = "bar", chartRangeMin = 0, chartRangeMax = 12)
})
Then the table renders fine.
From the reactable's reference manual, this is the definition of cell
argument for colDef
function:
cell
: Custom cell renderer. An R function that takes the cell value, row index, and column name as arguments, or a JS() function that takes a cell info object as an argument.
I don't see what is wrong. Could you help me spot my mistake please?
Best regards.
Silly mistake, sparkline
needs a number list and not a character list.
Change:
df <- orders %>%
dplyr::mutate(Date = format(Date, "%Y-%m-%d")) %>%
dplyr::group_by(PartName) %>%
tidyr::complete(Date = c('2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01', '2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01','2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01'), fill = list(Size = 0)) %>%
as.data.frame() %>%
dplyr::group_by(PartName) %>%
dplyr::summarize(ORDRS = paste0(Size, collapse = ",")) %>%
dplyr::group_by(PartName) %>%
dplyr::summarise(ORDRS = list(ORDRS))
For:
df <- orders %>%
dplyr::mutate(Date = format(Date, "%Y-%m-%d")) %>%
dplyr::group_by(PartName) %>%
tidyr::complete(Date = c('2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01', '2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01','2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01'), fill = list(Size = 0)) %>%
as.data.frame() %>%
dplyr::group_by(PartName) %>%
dplyr::summarise(ORDRS = list(Size))