强制updateSelectizeInput在更改事件上触发

时间:2019-01-10 13:06:12

标签: r shiny reactive

这是我的应用程序

library(shiny)
library(shinydashboard)
library(dplyr) 
data <- mtcars

ui <- dashboardPage(
    dashboardHeader(),
    dashboardSidebar(
        selectizeInput("cyl", "MPG", choices = c(4, 6, 8), selected = 6),
        selectizeInput("carb", "Carb", choices = c(1,2,3,4), selected = c(1,2,3), multiple = TRUE),
        uiOutput("carbui")
    ),
    dashboardBody(
        dataTableOutput("dt"),
        textOutput("carbsoutput")
    )
)

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

    # filtereddata <-  reactive({
    filtereddata <-  eventReactive(input$carb, {

        req(input$carb)
        data <- data %>%
            filter(cyl == input$cyl) %>%
            filter(carb %in% input$carb)
        data
    })

    output$dt <- renderDataTable({
        print("reactive was triggered")
        filtereddata()
    })


    observeEvent(input$cyl, {

        print("observer was triggered")

        vals <- switch(input$cyl,
                       "4" = c(1,2,3),
                       "6" = c(1,2,3,4),
                       "8" = c(1,2,3))

        updateSelectizeInput(session, "carb", "Carb", choices = vals, selected = vals)
    })

}

shinyApp(ui, server)

我试图达到的目的是,当我更新input$cyl时该表不会被双重渲染,这是在我将input$cyl从4切换到6而没有eventReactive时发生的。但是问题是,当我现在将input$cyl从4切换到8时,eventReactive不会被触发,因为input$carb不会被更新。有没有一种方法可以强制updateSelectizeInput进行更新,以便始终触发其他事件?

0 个答案:

没有答案