更新依赖的输入链
update dependent chain of inputs
我有一个相当复杂的用户界面,其中包含长链的相关用户输入。
我想添加的一个功能是能够在任何给定时间将所有输入值存储为字符串,并使用该字符串重置所有输入作为 actionButton 的结果。将字符串存储为 JSON 很容易,但事实证明恢复相关输入很棘手,我想知道是否有什么好主意。
下面提供了一个小示例应用程序。您可以看到变量选择取决于所选组(group1 或 group2)。
单击操作按钮后,我想设置组 = 组 2,变量 = group2_variable2。
但点击只准确设置组,变量设置为第一个(默认)选择。
我想我必须等到独立输入完全重置后才能更新任何相关输入,但是有没有一种系统的方法可以在一次观察()中完成所有这些操作?或者还有其他首选方法吗?
library(shiny)
groups <- c('group1', 'group2')
variables <- list(
group1 = c('group1_var1', 'group1_var2'),
group2 = c('group2_var1', 'group2_var2'))
ui <- pageWithSidebar(
# Application title
headerPanel('Demo'),
# Sidebar with a slider input for number of bins
sidebarPanel(
selectInput(
inputId = 'group',
label = 'Group:',
choices = groups),
uiOutput('variable_selector'),
actionButton(
inputId = 'reset_parameters',
label = 'Reset Parameters')
),
# Show a plot of the generated distribution
mainPanel(
h4('Demo')
)
)
server <- shinyServer(function(input, output, session) {
output$variable_selector <- renderUI({
selectInput(
inputId = 'variable',
label = 'Variable:',
choices = variables[[input$group]])
})
observe({
if (input$reset_parameters > 0) {
updateSelectInput(
session,
inputId = 'group',
selected = 'group2')
updateSelectInput(
session,
inputId = 'variable',
selected = 'group2_var2')
}
})
})
shinyApp(ui = ui, server = server)
我认为问题出在updateSelectInput
中给出的inputId
。
尝试使用 variable_selector
而不是 variable
。
我认为问题在于您的输入有选项 'group2_var1', 'group2_var2'
,但是当您重置它们时,您试图将其设置为 'group2_variable2'
。尝试以下更改...
updateSelectInput(
session,
inputId = 'variable',
selected = 'group2_var2')
您在此处使用 renderUI
,因此 variable
输入已正确重置,但随后会重新创建整个 selectInput
。
为避免这种情况,您可以使用 updateSelectInput
填充 variable
输入的 choices
。
在您的 ui.R
中,您可以将 uiOutput('variable_selector')
替换为:
selectInput(inputId = 'variable',
label = 'Variable:',
choices = "")
并且在 server.R
中:
observeEvent(input$group,{updateSelectInput(
session,
inputId = 'variable',
choices = variables[[input$group]])
})
当您按下按钮时,observeEvent
中的 updateSelectInput
会重新运行,但它不会从头开始重新创建 selectInput
,只会更改 choices
,因此所选选项保持不变。
我有一个相当复杂的用户界面,其中包含长链的相关用户输入。
我想添加的一个功能是能够在任何给定时间将所有输入值存储为字符串,并使用该字符串重置所有输入作为 actionButton 的结果。将字符串存储为 JSON 很容易,但事实证明恢复相关输入很棘手,我想知道是否有什么好主意。
下面提供了一个小示例应用程序。您可以看到变量选择取决于所选组(group1 或 group2)。
单击操作按钮后,我想设置组 = 组 2,变量 = group2_variable2。
但点击只准确设置组,变量设置为第一个(默认)选择。
我想我必须等到独立输入完全重置后才能更新任何相关输入,但是有没有一种系统的方法可以在一次观察()中完成所有这些操作?或者还有其他首选方法吗?
library(shiny)
groups <- c('group1', 'group2')
variables <- list(
group1 = c('group1_var1', 'group1_var2'),
group2 = c('group2_var1', 'group2_var2'))
ui <- pageWithSidebar(
# Application title
headerPanel('Demo'),
# Sidebar with a slider input for number of bins
sidebarPanel(
selectInput(
inputId = 'group',
label = 'Group:',
choices = groups),
uiOutput('variable_selector'),
actionButton(
inputId = 'reset_parameters',
label = 'Reset Parameters')
),
# Show a plot of the generated distribution
mainPanel(
h4('Demo')
)
)
server <- shinyServer(function(input, output, session) {
output$variable_selector <- renderUI({
selectInput(
inputId = 'variable',
label = 'Variable:',
choices = variables[[input$group]])
})
observe({
if (input$reset_parameters > 0) {
updateSelectInput(
session,
inputId = 'group',
selected = 'group2')
updateSelectInput(
session,
inputId = 'variable',
selected = 'group2_var2')
}
})
})
shinyApp(ui = ui, server = server)
我认为问题出在updateSelectInput
中给出的inputId
。
尝试使用 variable_selector
而不是 variable
。
我认为问题在于您的输入有选项 'group2_var1', 'group2_var2'
,但是当您重置它们时,您试图将其设置为 'group2_variable2'
。尝试以下更改...
updateSelectInput(
session,
inputId = 'variable',
selected = 'group2_var2')
您在此处使用 renderUI
,因此 variable
输入已正确重置,但随后会重新创建整个 selectInput
。
为避免这种情况,您可以使用 updateSelectInput
填充 variable
输入的 choices
。
在您的 ui.R
中,您可以将 uiOutput('variable_selector')
替换为:
selectInput(inputId = 'variable',
label = 'Variable:',
choices = "")
并且在 server.R
中:
observeEvent(input$group,{updateSelectInput(
session,
inputId = 'variable',
choices = variables[[input$group]])
})
当您按下按钮时,observeEvent
中的 updateSelectInput
会重新运行,但它不会从头开始重新创建 selectInput
,只会更改 choices
,因此所选选项保持不变。