R:检查是否以及在observe()中单击了哪个下拉菜单

R:检查是否以及在observe()中单击了哪个下拉菜单,r,shiny,R,Shiny,我目前正在构建一个闪亮的应用程序,用户可以根据五个下拉菜单过滤数据。这些菜单的行为应类似于Excel的过滤功能,例如:选择菜单#1中的选项,根据第一个菜单的输入过滤其他四个菜单的数据。选择菜单#2中的选项,再次过滤菜单的所有其他输入,但(这非常重要)保持菜单#1的输入处于选中状态,并根据新输入进行过滤 在第一次尝试中,我用五个observeEvent()函数对这个结构进行了编码,每个下拉菜单对应一个函数。这是可行的,但是产生了大量的代码,因为过滤器和更新机制相当大 我目前正在修改我的代码,我想我

我目前正在构建一个闪亮的应用程序,用户可以根据五个下拉菜单过滤数据。这些菜单的行为应类似于Excel的过滤功能,例如:选择菜单#1中的选项,根据第一个菜单的输入过滤其他四个菜单的数据。选择菜单#2中的选项,再次过滤菜单的所有其他输入,但(这非常重要)保持菜单#1的输入处于选中状态,并根据新输入进行过滤

在第一次尝试中,我用五个observeEvent()函数对这个结构进行了编码,每个下拉菜单对应一个函数。这是可行的,但是产生了大量的代码,因为过滤器和更新机制相当大

我目前正在修改我的代码,我想我可以将五个observeEvent()函数折叠成一个observeEvent()函数,因为过滤器机制的主要部分对于所有五个菜单都完全相同

我现在遇到的问题是,我需要一个函数来告诉我哪个输入字段已更改。flush事件现在检查observe()函数中的所有输入字段。不能使用is.null()的测试,因为空下拉菜单也是需要检查的“更改”(例如,用户改变了主意,现在不想使用菜单#1进行筛选)

出现错误的示例代码以及类似“is.active”或“ic.clicked”的函数非常有用:

observe({

#reset values on each flush event
which_input <<- ""
bmodel_content <<- ""
country_content <<- ""
company_content <<- ""

#check which input has changed
#save the name and content for later update statements
if(!is.null(input$bmodel))
{
  bmodel_content <<- input$bmodel
  which_input <<- "bmodel"
}
else if(is.null(input$bmodel))
{
  bmodel_content <<- c(unique(data$business.model))
  which_input <<- "bmodel"
}
if(!is.null(input$country))
{
  country_content <<- input$country
  which_input <<- "country"
}
else if(is.null(input$country))
{
  country_content <<- c(unique(data$country))
  which_input <<- "country"
}

(... another 3 if-else statements for the other 3 menus ...)

###
#ERROR: "which_input" should be set with the name of the changed input field
#       now it is always set to the last if-else-statement
###

(... filter data and updateSelectizeInput statements...)
观察({
#重置每个刷新事件的值

which_input这可能不是对您问题的直接回答,因为我不知道您的筛选函数是什么样子的。但是在下面的示例中,您不需要知道哪个输入正在更改,但您仍然可以按预期获得筛选

library(shiny)
library(dplyr)

ui <- shinyUI(fluidPage(

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel(
        selectInput("cyl", "cyl", unique(mtcars$cyl), multiple = TRUE),
        selectInput("vs", "vs", unique(mtcars$vs), multiple = TRUE),
        selectInput("am", "am", unique(mtcars$am), multiple = TRUE)
      ),

      mainPanel(
         tableOutput("table")
      )
   )
))

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

    output$table <- renderTable({
        cyl_sel <- if (is.null(input$cyl)) unique(mtcars$cyl) else as.numeric(input$cyl)
        vs_sel <- if (is.null(input$vs)) unique(mtcars$vs) else as.numeric(input$vs)
        am_sel <- if (is.null(input$am)) unique(mtcars$am) else as.numeric(input$am)
        filter(mtcars, cyl %in% cyl_sel, vs %in% vs_sel, am %in% am_sel)
    })

})

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(dplyr)

ui这实际上取决于筛选函数的外观,因为您可能根本不需要使用输入的
。例如,
dplyr
中的
filter()
可能是一个不错的选择,您可以将所有条件列为参数,而不必担心其中哪一个正在更改。