首页 > 解决方案 > 使用 rhandsontable 但不使用 dq_render_handsontable 的反应数据表

问题描述

我正在尝试构建一个 rhandsontable,但这次使用 dq_render_handsontable,因为它提供了额外的功能来放置漂亮的过滤器和分页。在我的应用程序中,我根据选择器输入对数据进行了两次过滤,这将创建两个反应性数据帧... filter_tbl_1() 在第一次拟合之后和 filter_tbl_2() 在第二次之后。

dq_render_handsontable 函数适用于 filter_tbl_1() 数据并提供正确的过滤。但是使用 filter_tbl_2() 会引发错误。我的猜测是它无法识别 filter_tbl_2()。不仅如此,错误中没有显示任何信息。如果有人可以提供帮助将不胜感激

最小可重现代码:

library(dqshiny)
library(rhandsontable)
library(tibble)
library(dplyr)

ui <- fluidPage(
    column(
        width = 4,
        pickerInput(inputId = "carb_input", 
                    label = "Pick carb",
                    choices = mtcars %>% pull(carb) %>% unique(),
                    selected = mtcars %>% pull(carb) %>% unique(),
                    multiple = TRUE,
                    options = pickerOptions(
                        actionsBox = TRUE,
                        liveSearch = TRUE,
                        size = 10
                    )),
        pickerInput(inputId = "gear_input", 
                    label = "Pick Gear",
                    choices = NULL,
                    multiple = TRUE,
                    options = pickerOptions(
                        actionsBox = TRUE,
                        liveSearch = TRUE,
                        size = 10
                    ))
    ),
    column(
        width = 8,
        dq_handsontable_output("review_table", 8L),
        rHandsontableOutput("test")
    )


)

server <- function(input,output,session){
    filter_tbl_1 <- reactive({
        mtcars %>% rowid_to_column() %>% filter(carb %in% input$carb_input)
    })

    observeEvent(filter_tbl_1(),{
        choices <- filter_tbl_1() %>% pull(gear) %>% unique()
        updatePickerInput(session, "gear_input", choices = choices, selected = choices)
    })

    filter_tbl_2 <- reactive({
        req(input$gear_input)
        filter_tbl_1() %>% filter(gear %in% input$gear_input)
    })



    dq_render_handsontable("review_table",
                           data = reactive(filter_tbl_1()))

    output$test <- renderRHandsontable(rhandsontable(filter_tbl_2()))
}

shinyApp(ui,server)

dq_render_handsontable("review_table", data = reactive(filter_tbl_1()))行中,如果将filter_tbl_1()替换为filter_tbl_2(),您将看到错误。

标签: shinyshiny-reactivityrhandsontable

解决方案


推荐阅读