首页 > 解决方案 > 闪亮:updateSelectInput() 选择的参数问题与observe()

问题描述

在用户在分类下拉列表中选择后,我observe()用来更改 a 的值。在我程序的第一个选项卡中,如果您将分类设置为,则Impute将更新为,否则。然后,我可以根据需要更改Impute值,而不会恢复为选中时出现的值。selectInputTRUE/FALSETRUEmodemeanTRUE/FALSE

在第二个选项卡中,我有一个selectInput与第一个选项卡具有相似界面的多个列表;该接口是为在Select covariates中选择的每个值创建的。在本节中,我还根据第一个选项卡的逻辑observe()来更新每个选定的协变量的Impute下拉列表(即,如果TRUE选择,则Impute更新为modemean否则更新)。但是,从某种意义上说,Impute 中的值似乎被锁定,因为我无法像在第一个选项卡中那样在值之间切换。

我不知道如何纠正这个问题,我想知道是否有人遇到过类似的问题并且能够解决它。任何建议或帮助将不胜感激。

我的应用程序的代码如下所示,可以在单个文件中运行。

library(shiny)
library(shinyjs)

ui <- shinyUI(fluidPage(
  shinyjs::useShinyjs(),
  navbarPage("Test",id="navbarPage",
             tabPanel("First tab", id = "first_tab",
                      sidebarLayout(
                        sidebarPanel(
                          selectInput('covariate.L.categorical', 'Categorical', c("",TRUE,FALSE)),
                          selectInput('covariate.L.impute', "Impute", c("","default","mean","mode","median"))
                        ),
                        mainPanel()
                      )
             ),
             tabPanel("Second tab", id = "second_tab",
                      sidebarLayout(
                        sidebarPanel(
                          selectInput('covariates', 'Select covariates', choices = c("age","sex","race","bmi"), multiple=TRUE, selectize=TRUE), 
                          tags$hr(),
                          uiOutput("covariateop")
                        ),
                        mainPanel()
                      )
             ))
))

server <- shinyServer(function(input, output, session) {
  
  rv <- reactiveValues(cov.selected = NULL)
  
  observe({
    updateSelectInput(session, "covariate.L.impute", selected = ifelse(input$covariate.L.categorical,"mode","mean"))
  })
  
  output$covariateop <- renderUI({  
    lapply(input$covariates, function(x){
      
      tags$div(id = paste0("extra_criteria_for_", x),
               h4(x),
               selectInput(paste0(x,"_categorical"), "Categorical",
                           choices = c("",TRUE,FALSE)),
               selectInput(paste0(x,"_impute"), "Impute",
                           choices = c("","default","mean","mode","median")),
               textInput(paste0(x,"_impute_default_level"), "Impute default level"),
               tags$hr()
      )
    })
  })
  observe({
    lapply(input$covariates, function(x){
      updateSelectInput(session, paste0(x,"_impute"), selected = ifelse(as.logical(reactiveValuesToList(input)[[paste0(x,"_categorical")]])==TRUE,"mode","mean"))
    })
  })
})

# Run the application
shinyApp(ui = ui, server = server)

标签: rshiny

解决方案


observe第二个选项卡中,您使用reactiveValuesToList(input)[[paste0(x,"_categorical")]]. 这意味着这个观察对任何input元素的任何变化都有反应,如果你改变“插补”输入也是如此。您可以input[[paste0(x,"_categorical")]]改用它来摆脱这种行为。

Note that the implementation of dynamic UI with lapplyleads to the deletion and anew rendering of already existing input selections when an additional variable is selected. 也许你可以看看insertUI/removeUI以获得更好的用户界面。


推荐阅读