在Shiny,R中按顺序生成动态用户界面

时间:2016-08-27 21:49:27

标签: r shiny selectize.js

我在R中创建了一个动态用户界面,它在流体行环境中生成四个selectizeInput框,其中一组观察者正在观察输入。如果选择了addFilter按钮,则会生成另外四个框的额外流体行。

我的问题围绕着保存以前输入的值。我有一组存储用户输入的无效值。然后将这些值输入updateselectize的观察者。如果用户点击缓慢,则一切正常。但是,如果用户快速点击,有时会删除这些值。

我相信这是因为当脚本设置新界面时,带有观察者的旧界面还没有完全呈现;但值已重置。

以下是MWE。请注意,要触发错误,您需要快速双击“添加过滤器”按钮。

Server.R:

library(shiny)

shinyServer(function(session, input, output) {

  rValues <- reactiveValues(filter_counter = 1, filter_counter2 = 1, filter_waiter = 1, savedFil_1 = "", savedOp_1 = "",
                            savedCrit_1 = "", savedAO_1 = "")

  columns <- c("C1", "C2")

  operators <- c("O1", "O2")
  values <- c("V1", "V2")
  andor <- c("&&", "||")

  observeEvent(input$AddFilter,{
    if (rValues[['filter_waiter']] == 1) {
      # Step 1: Store 
      lapply(1:rValues$filter_counter, function(i) {
        rValues[[paste0("savedFil_", i)]] <- input[[paste0("fil_", i)]]
        rValues[[paste0("savedOp_", i)]] <- input[[paste0("filOperators_", i)]]
        rValues[[paste0("savedCrit_", i)]] <- input[[paste0("filCriteria_", i)]]
        rValues[[paste0("savedAO_", i)]] <- input[[paste0("andor_", i)]]
      })

      # Step 2: Increment counter
      rValues$filter_counter <- rValues$filter_counter + 1

      # Step 3: Set filter waiter
      rValues[['filter_waiter']] <- 0
    } 
  })

  output$filters <- renderUI({
    if (rValues[['filter_waiter']] == 1) {
      ui <- lapply(1:rValues$filter_counter, function(i) {
        fluidRow(
          column(4, selectizeInput(paste0("fil_", i), label = NULL, choices = NULL)),
          column(2, selectizeInput(paste0("filOperators_", i), label = NULL, choices = NULL)),
          column(4, selectizeInput(paste0("filCriteria_", i), label = NULL, choices = NULL)),
          column(2, selectizeInput(paste0("andor_", i), label = NULL, choices = NULL)))
      })
      return(ui)
    }
  })

  ### Create observers for filters
  observe({
    lapply(1:rValues$filter_counter, function(i){
      # Update the Selectizes
      updateSelectizeInput(session, paste0("fil_", i), selected = rValues[[paste0("savedFil_", i)]], choices = columns, server = TRUE)
      updateSelectizeInput(session, paste0("filOperators_", i), selected = rValues[[paste0("savedOp_", i)]], choices = operators, server = TRUE)
      updateSelectizeInput(session, paste0("filCriteria_", i), selected = rValues[[paste0("savedCrit_", i)]], choices = values, server = TRUE)
      updateSelectizeInput(session, paste0("andor_", i), selected = rValues[[paste0("savedAO_", i)]], choices =  c("", "&&", "||"), server = TRUE)
    })

    rValues[['filter_waiter']] <- 1
  })
})

UI.R:

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Dynamic SelectizeInput UI"),

  sidebarLayout(
    sidebarPanel(
      actionButton("AddFilter", label = "Add a Filter"),
      uiOutput("filters")
    ),

    mainPanel(
      h1("")
    )
  )
))

1 个答案:

答案 0 :(得分:1)

您的代码可以简化。哟不需要更新selectizeInput控件,因为每次选择按钮时都会生成新控件。 你有太多的反应变量。我改为全局变量。如果您不想使用全局变量,则使用带隔离的反应变量。

library(shiny)

filter_counter <- 0
filter_values  <- list()
columns        <- c("C1", "C2") 
operators      <- c("O1", "O2")
values         <- c("V1", "V2")
andor          <- c("&&", "||") 

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

  output$filters <- renderUI({  

      input$AddFilter  
      # Step 1: store filter values
      if ( filter_counter>0) {
          lapply(1:filter_counter, function(i) {
            filter_values[[paste0("savedFil_", i)]]  <<- input[[paste0("fil_", i)]]
            filter_values[[paste0("savedOp_", i)]]   <<- input[[paste0("filOperators_", i)]]
            filter_values[[paste0("savedCrit_", i)]] <<- input[[paste0("filCriteria_", i)]]
            filter_values[[paste0("savedAO_", i)]]   <<- input[[paste0("andor_", i)]]
          })
      }

      # Step 2: Increment counter 
      filter_counter <<-  filter_counter + 1 

      # Step 3: generate selectInputs
      ui <- lapply(1:filter_counter, function(i){ 
          fluidRow(
              column(4,selectizeInput(paste0("fil_", i), label= 'fil', selected = filter_values[[paste0("savedFil_", i)]], choices = columns )),
              column(2,selectizeInput(paste0("filOperators_", i),label= 'filop',  selected = filter_values[[paste0("savedOp_", i)]], choices = operators )),
              column(4,selectizeInput(paste0("filCriteria_", i),label= 'filcri',  selected = filter_values[[paste0("savedCrit_", i)]], choices = values )),
              column(2,selectizeInput(paste0("andor_", i),label= 'andor',  selected = filter_values[[paste0("savedAO_", i)]], choices =  c("", "&&", "||") ))
          )
      }) 

    })
})


ui <- shinyUI(fluidPage(

  # Application title
  titlePanel("Dynamic SelectizeInput UI"),

  sidebarLayout(
    sidebarPanel(
      actionButton("AddFilter", label = "Add a Filter"),
      uiOutput("filters")
    ),

    mainPanel(
      h1("")
    )
  )
))

shinyApp(ui , server)