我正在使用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))
答案 0 :(得分:1)
一种可能的解决方案是使用updateSelectInput
,因此两个具有相同di inputId的id没问题
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)