迷你图 returns 空白 table

Sparkline returns blank table

我正在尝试将迷你图添加到一个简单的 reactable 示例中,但是当我尝试这样做时 table returns 空白。

我一直在关注 this example from the documentation

这是我使用的示例数据:

    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)

这就是我传递给 reac 的数据的样子table:

这是闪亮的应用程序:

    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)

结果:

如果删除这部分:

    ORDRS = colDef(cell = function(values) {
        sparkline(values, type = "bar", chartRangeMin = 0, chartRangeMax = 12)
         })

然后 table 渲染正常。

来自 reactable's reference manual,这是 colDef 函数的 cell 参数的定义:

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.

我看不出有什么问题。你能帮我找出我的错误吗?

此致。

愚蠢的错误,sparkline 需要数字列表而不是字符列表。

变化:

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))

对于:

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))

现在显示正常: