【问题标题】:Shiny: updateSelectInput() selected argument issue with observe()闪亮:updateSelectInput() 选择的参数问题与observe()
【发布时间】:2020-12-16 19:09:07
【问题描述】:

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

在第二个选项卡中,我有一个多个selectInput 列表,其界面与第一个选项卡相似;该接口是为选择协变量中选择的每个值创建的。在本节中,我还使用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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

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

    请注意,使用 lapply 实现动态 UI 会导致在选择其他变量时删除并重新呈现已存在的输入选择。也许你可以看看insertUI/removeUI 以获得更好的用户界面。

    【讨论】:

      猜你喜欢
      • 2019-02-15
      • 2020-05-11
      • 2020-05-24
      • 1970-01-01
      • 2016-02-21
      • 2018-09-20
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      相关资源
      最近更新 更多