r - 闪亮:updateSelectInput() 选择的参数问题与observe()
问题描述
在用户在分类下拉列表中选择后,我observe()
用来更改 a 的值。在我程序的第一个选项卡中,如果您将分类设置为,则Impute将更新为,否则。然后,我可以根据需要更改Impute值,而不会恢复为选中时出现的值。selectInput
TRUE/FALSE
TRUE
mode
mean
TRUE/FALSE
在第二个选项卡中,我有一个selectInput
与第一个选项卡具有相似界面的多个列表;该接口是为在Select covariates中选择的每个值创建的。在本节中,我还根据第一个选项卡的逻辑observe()
来更新每个选定的协变量的Impute下拉列表(即,如果TRUE
选择,则Impute更新为mode
,mean
否则更新)。但是,从某种意义上说,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)
解决方案
在observe
第二个选项卡中,您使用reactiveValuesToList(input)[[paste0(x,"_categorical")]]
. 这意味着这个观察对任何input
元素的任何变化都有反应,如果你改变“插补”输入也是如此。您可以input[[paste0(x,"_categorical")]]
改用它来摆脱这种行为。
Note that the implementation of dynamic UI with lapply
leads to the deletion and anew rendering of already existing input selections when an additional variable is selected. 也许你可以看看insertUI
/removeUI
以获得更好的用户界面。
推荐阅读
- r - 使用 for 循环从均匀分布中确定两点之间的距离
- c# - 使用 LINQ 进行分页并传递参数进行搜索
- javascript - JavaScript“button.disabled = boolean”无法正常工作
- fortran - 在打开的 fortran-90 语句中制作修剪方法不起作用
- matplotlib - Matplotlib:用箱线图替换散点图轴
- api - (ssg-wsg) 有没有办法从 api 获得完整的课程参考号?
- multithreading - 如何正确计算并行总和?
- node.js - 在猫鼬中获得共同的追随者
- python - 如果您两次导入一个模块,一次在顶级脚本中,一次在顶级脚本导入的模块之一中,会发生什么情况?
- wkhtmltopdf - 如何修复 wkhtmltopdf 中的 SVG 颜色渐变渲染