【问题标题】:Produce two shiny widgets from the same uiOutput() at the same time同时从同一个 uiOutput() 生成两个闪亮的小部件
【发布时间】:2020-09-14 18:41:16
【问题描述】:

我有下面闪亮的仪表板,我从pickerInput() 中选择"Select Database/s",如果我将显示Sepal.Length 的值(如果仅选择此选项)或Sepal.Width(如果仅选择此选项)或两者如果两者都选择选择。是否可以在同一个uiOutput("fnamesid") 中实现它而不必使用两个不同的uiOutput()

library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui <- dashboardPage(
    header = dashboardHeader(title = "My dashboard"),
    sidebar = dashboardSidebar(
        uiOutput("dbs"),
        uiOutput("fnamesid")
    ),
    body = dashboardBody(

    )
)

server <- function(input, output, session) {
    output$dbs<-renderUI({

            pickerInput("DB", "Select Database/s", 
                        choices = c("Sepal.Length","Sepal.Width"), 
                        multiple = T,options = list(`actions-box` = TRUE),
                        selected = "Sepal.Length")

    })
    output$fnamesid<-renderUI({

            if("Sepal.Length"%in% input$DB){
                pickerInput("sn", "Select sn_ID for x-axis", 
                            choices = unique(iris$Sepal.Length), 
                            multiple = T,options = list(`actions-box` = TRUE),
                            selected = head(unique(iris$Sepal.Length)))

            }
        else if("Sepal.Width"%in% input$DB){
            pickerInput("sn2", "Select sn_ID for x-axis", 
                        choices = unique(iris$Sepal.Width), 
                        multiple = T,options = list(`actions-box` = TRUE),
                        selected = head(unique(iris$Sepal.Width)))

        }
        else if("Sepal.Length"%in% input$DB&"Sepal.Width"%in% input$DB){
            pickerInput("sn", "Select sn_ID for x-axis", 
                        choices = unique(iris$Sepal.Length), 
                        multiple = T,options = list(`actions-box` = TRUE),
                        selected = head(unique(iris$Sepal.Length)))


            pickerInput("sn2", "Select sn_ID for x-axis", 
                        choices = unique(iris$Sepal.Width), 
                        multiple = T,options = list(`actions-box` = TRUE),
                        selected = head(unique(iris$Sepal.Width)))

        }



    })

}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您的问题是,如果在input$DB 中选择了这两个选项,那么第一个if () 语句的条件是TRUE,这就是您的if-then-else 链中被执行的元素。所以,首先要做的就是将链中的“都被选中”元素移到顶部,并相应地调整你的逻辑。

    其次,要从对renderUI() 的同一调用中返回两个(或更多)UI 元素,请将它们包装在tagList() 中。这是您的 output$fnamesid 的一个版本,适用于我:

      output$fnamesid<-renderUI({
        if ("Sepal.Length" %in% input$DB & "Sepal.Width" %in% input$DB){
          tagList(
            pickerInput("sn", "Select sn_ID for x-axis", 
                      choices = unique(iris$Sepal.Length), 
                      multiple = T,options = list(`actions-box` = TRUE),
                      selected = head(unique(iris$Sepal.Length))),
            pickerInput("sn2", "Select sn_ID for x-axis", 
                      choices = unique(iris$Sepal.Width), 
                      multiple = T,options = list(`actions-box` = TRUE),
                      selected = head(unique(iris$Sepal.Width)))
          )
        }
        else if("Sepal.Length"%in% input$DB){
          pickerInput("sn", "Select sn_ID for x-axis", 
                      choices = unique(iris$Sepal.Length), 
                      multiple = T,options = list(`actions-box` = TRUE),
                      selected = head(unique(iris$Sepal.Length)))
        }
        else if("Sepal.Width"%in% input$DB){
          pickerInput("sn2", "Select sn_ID for x-axis", 
                      choices = unique(iris$Sepal.Width), 
                      multiple = T,options = list(`actions-box` = TRUE),
                      selected = head(unique(iris$Sepal.Width)))      
        }
    

    注意tagList 中两个pickerInputs 之间的逗号。这一点很重要! ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 2014-08-16
      • 2015-08-03
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多