我正在尝试将值组合到一个selectInput中,以便将其传递给另一个函数。下面是我的代码,用户选择第一个输入,值应该是survey = 1和youth = FALSE。
library(shiny)
output$choose_wave <- renderUI({
selectInput("selected_wave", "Wave", choices = list(
"Wave 1- Adult" = wave = 1, youth = FALSE,
"Wave 1- Youth" = wave = 1, youth = TRUE,
"Wave 2- Adult" = wave = 2, youth = FALSE,
"Wave 2- Youth" = wave = 2, youth = TRUE
))
})用一个输入传递两个值的更好方法是什么?
发布于 2018-05-24 01:18:55
我认为selectizeInput或pickerInput ( shinyWidgets包的一部分)都能满足您的要求。这是一个使用pickerInput的示例,您可以将这两种因素分成组,并允许进行多个选择,最终将这些选择组合到输出中。下面是一个最小的例子。
devtools::install_github("dreamRs/shinyWidgets")
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
uiOutput("choose_wave"),
h5("Value from selected wave:"),
verbatimTextOutput("select_values")
)
server <- function(input,output) {
output$choose_wave <- renderUI({
pickerInput("selected_wave", "Select one from each group below:",
choices = list(Wave = c("Wave 1" = 1, "Wave 2" = 2), Youth = c("Adult"=FALSE, "Youth"=TRUE)),
multiple = T, options = list('max-options-group' = 1))
})
output$select_values <- renderText({as.character(input$selected_wave)})
}
shinyApp(ui, server)更新:,shinyWidgets的开发版本,现在允许限制每个组的选择数量(感谢维克多!)。上面的代码已经更新,将选择限制为每个组一个,但您需要使用开发版本才能正常工作。
菜单布局:

值:

发布于 2018-05-23 03:18:42
你能使用if/else吗?可能更笨重,但它是有效的。同样做输入的工作,我通常为文本输入赋值。
if(input$wave==1 #or whatever the input is actually equal too){
wave = 1
youth = FALSE
}else if(input$wave==2){
wave = 1
youth = TRUE
} and so on..我会说,首先将输入赋给一个值,然后再分配所需的额外数据。
https://stackoverflow.com/questions/50474865
复制相似问题