0
votes

I have the shiny dashboard below in which I select from the pickerInput() "Select Database/s" if I will display the values of either Sepal.Length if only this is selected or Sepal.Width if only this is selected or both if both selected. Is it possible to achieve it inside the same uiOutput("fnamesid") and without having to use 2 different 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)
1

1 Answers

1
votes

Your problem is that if both options are selected in input$DB, then the condition of the first if () statement is TRUE and that is the element of your if-then-else chain that gets executed. So, the first thing to do is to move the "both are selected" element of the chain to the top and adjust your logic accordingly.

Second, to return two (or more) UI elements from the same call to renderUI(), wrap them in a tagList(). Here's a version of your output$fnamesid that works for me:

  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)))      
    }

Notice the comma between the two pickerInputs in the tagList. It's important! ;)