刷新过滤器和 Table

Refreshing Filter and Table

我有以下代码:

library(shiny)
library(shinydashboard)
library(rhandsontable)

header <- dashboardHeader(title = "Sample", titleWidth = 375)

sidebar <- dashboardSidebar(width = 270,
                            sidebarMenu(id="mymenu",
                                        menuItem(text = "Home", tabName = "tabCars", icon = icon("home", class="home"))
                            ))

body <- dashboardBody (
  tabItems(
    tabItem(tabName = "tabCars",

        fluidRow(

          column(width = 2,
                 selectInput(
                   inputId = "selected_CarCylinders",
                   label = "Car Cylinders",
                   choices = mtcars$cyl,
                   selectize = TRUE,
                   width = "250px",
                   multiple = FALSE
                 )),

          column(width = 2, style = "margin-top: 25px",
                 actionButton("deleteBtn", "Delete Selected Cylinders")),

          column(width = 1, style = "margin-top: 25px",
                 actionButton("refreshBtn", "Refresh Filter/Chart")),

          rHandsontableOutput("carDT")

        )
    )
  )
)

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output, session) {

  output$carDT <- renderRHandsontable({
    df <- mtcars
    rhandsontable(df, stretchH = "all")
  })

  observeEvent(input$deleteBtn, {
    # need help here
  })

  observeEvent(input$refreshBtn, {
    # need help here    
  })

}

shinyApp(ui, server)

我需要帮助编写将进入服务器端的 input$deleteBtn 和 input$refreshBtn 部分的内容。如果您 运行 按原样编写代码,想法是 select mtcars 中的汽缸数,然后单击删除按钮从 table 中删除所有这些条目并进行过滤;但是,过滤器和 table 只会在单击刷新按钮后更新。

虽然 permanently delete 向我尖叫一个 SQLite 数据库,但您可以通过使用 reactiveVal 存储数据框并调用 req 来实现这一点单击 refreshBtn 时刷新 table(在这种情况下,您还必须单击它以在应用程序开始时显示 table)。

server <- function(input, output, session) {
  # Create a `reactiveVal` and set a value to it
  df <- reactiveVal()

  df(mtcars)

  output$carDT <- renderRHandsontable({
    req(input$refreshBtn)

    rhandsontable(df(), stretchH = "all")
  })

  observeEvent(input$deleteBtn, {
    data <- dplyr::filter(df(), cyl != input$selected_CarCylinders)

    # Update `selectInput` to filter out the choices too (for good measure)
    updateSelectInput(session, "selected_CarCylinders", choices = data$cyl)

    # Update the `reactiveVal` value
    df(data)
  })
}