[英]Shiny - conditional panel not recognizing input
我正在使用shiny
并且在使用conditionalPanel
时很难让 R 识别输入。 我的应用程序中有两个这样的面板,因为我需要它们根据用户选择的单选按钮生成特定列表。
从 UI 的角度来看,一切都呈现得很好,但由于某种原因,当“名称”选项通过条件面板时,R 无法接收任何输入,即使在字段中输入了一个项目。 使用了正确的列表,但由于某种原因它没有检测到任何输入。 这是选择和结果的屏幕截图。
如果我从单选按钮中选择另一个选项并输入一个项目,我没有问题 - 它按预期工作。 这是我正在使用的代码。
ui <- fluidPage(
sidebarPanel(
# the user can choose from two options within the radio buttons
radioButtons("items_type", label = "Choose items for analysis"
, choices = list("Items - Item number & name" = "Both", "Items - Item name only" = "Name")
, selected = "Both")
# if the 'Both' option is selected, the items_list_names_id option is used
, conditionalPanel(
condition = "output.items_type == 'Both'"
, selectInput("items", label = NULL, choices = items_list_names_id, multiple = TRUE))
# if the 'Name' option is selected, the items_list_names option is used. input
# is not being detected here for some reason, and I'm wondering if it's because
# I use "items" for both selectInputs
, conditionalPanel(
condition = "output.items_type == 'Name'"
, selectInput("items", label = NULL, choices = items_list_names, multiple = TRUE))
# action button so the user can submit for analysis based on their selected options
, actionButton("go", "Run", style = "color: white; background-color: #2759aa")
)
)
server <- function(input, output){
# this portion is used to detect if the user alternates between either option from the radio buttons and uses the appropriate selectInput option
output$items_type <- reactive({
input$items_type
})
outputOptions(output, "items_type", suspendWhenHidden = FALSE)
Results <- eventReactive(input$go, {
# this portion is simply for testing for me to see how R recognizes the inputs
observe({print(input$items_type)})
observe({print(input$items)})
# checks to make sure the user selects at least 1 item. For some reason,
# this portion is triggered when at least 1 item is selected under the 'Name'
# condition from the conditional panel.
validate(
need(input$items > 0, 'Please select at least 1 item for analysis')
)
#other steps start here, but the above is the more pertinent part
}
编辑:所以看起来两个selectInput
选项具有相同的输入 id 是导致 R 在条件面板之间切换时无法识别输入的原因。 但是,最好有一个输入 id,因为intput$item
用于我上面未显示的代码的其他部分。 重写代码使其使用两个变量,例如input$item1
和input$item2
,对于每个条件可能会非常麻烦。 我愿意接受任何建议以避免这种情况。
编辑 2 :我在想也许使用单个conditonalPanel
并使用 switch 语句根据用户的选择在两个列表之间交替。 从理论上讲,这应该可行,并且是一个方便的解决方案,而无需修改我的所有其他代码。 它看起来像这样:
, conditionalPanel(
condition = "output.items_list_selection"
, selectInput("items", label = 'Select items'
, choices = switch("output.items_list_selection", "Both" = items_list_names_id, "Name" = items_list_names)
, multiple = TRUE))
一个可能的解决方案是使用updateSelectInput
,因此两个 id 具有 samedi inputId 没有问题
library(shiny)
items_list_names_id = c("id1", "id2")
items_list_names = c('name1', 'name2')
ui <- fluidPage(
sidebarPanel(
# the user can choose from two options within the radio buttons
radioButtons("items_type", label = "Choose items for analysis"
, choices = list("Items - Item number & name" = "Both", "Items - Item name only" = "Name")
, selected = "Both"),
# if the 'Both' option is selected, the items_list_names_id option is used
selectInput("items", label = NULL, choices = c('init'), multiple = TRUE),
# action button so the user can submit for analysis based on their selected options
actionButton("go", "Run", style = "color: white; background-color: #2759aa")
)
)
server <- function(input, output, session){
# change the choices depending on the value of input$intems_type
observeEvent(input$items_type, {
if(input$items_type == 'Both'){
updateSelectInput(session, 'items', label = NULL, choices = items_list_names)
}
if(input$items_type == 'Name'){
updateSelectInput(session, 'items', label = NULL, choices = items_list_names_id)
}
})
# check if it's working
observeEvent(input$go,{
print(input$items)
print(input$items_type)
})
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.