1

the given script creates the attached snapshot.It is a table created using DT package in R. I want to make the menu above the table such that by selecting an input "A" in the first SelectInput, I get the second selectInput with two sliders, while selecting "B" in the first SelectInput, I should get only the second SelectInput and no sliders. There is no change needed on the table iris. Please help and Thanks.

## app.R ##
library(shiny)
library(shinydashboard)
library(DT)

#Declaring the UI
ui <- fluidPage(
titlePanel("Interactive Menu"),

# Create a new Row in the UI for selectInputs
fluidRow(
column(3,
     selectInput("names",
                 "Customer:",
                 c("A","B"))
)),
fluidRow(

column(4,
       selectInput("names",
                   "Customer:",
                   c(as.character(iris$Species)))
),
column(4,
       sliderInput("slide", "Select the slider one",
                   min = 75, max = 100,
                   value = 75, step = 5)

),
column(4,

       sliderInput("city", "Select the slider two",
                   min = 60, max = 100,
                   value = 60, step = 10)
)),

# Create a new row for the table.
fluidRow(
DT::dataTableOutput("table1")
)
)
#Declaring the Server
server <- function(input, output) {

# Filter data based on selections
output$table1 <- renderDataTable({

datatable(iris, options = list(
  searching = FALSE,
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))
})
}
shinyApp(ui, server)

iris_capture

SBista
  • 7,479
  • 1
  • 27
  • 58
Ashmin Kaul
  • 860
  • 2
  • 12
  • 37
  • 1
    Possible duplicate of [R Shinydashboard Showing/Hiding UI Elements based on Tab selection](https://stackoverflow.com/questions/39987908/r-shinydashboard-showing-hiding-ui-elements-based-on-tab-selection) – Hardik Gupta Oct 16 '17 at 05:39
  • Hi Hardik, Thanks, I have worked on this functionality, my question is, that can we implement the selectInput with conditional panel in the same way like the tabset conditional panel that this links says,also, if you have a working example, kindly share. – Ashmin Kaul Oct 16 '17 at 05:57
  • You could either use `conditionalPanel` or `uiOutput` with `renderUI`. – SBista Oct 16 '17 at 06:27
  • I want to add multiple selectInputs and sliders using conditioalPanel, do you happen to have a good link for a working example? – Ashmin Kaul Oct 16 '17 at 06:34
  • Thank you so much SBista and Hardik for your help. – Ashmin Kaul Oct 16 '17 at 07:14
  • You could look into [shiny reference page from RStudio](https://shiny.rstudio.com/reference/shiny/latest/conditionalPanel.html) – SBista Oct 16 '17 at 07:31
  • Hi SBista, please help with this link: https://stackoverflow.com/questions/46783181/sliderinput-issue-with-table-in-r-shiny – Ashmin Kaul Oct 17 '17 at 06:17

2 Answers2

0

You can add multiple widgets like so:

rm(list = ls())
library(shiny)
runApp(list(
  ui = bootstrapPage(
    selectInput('data', 'Data', c('mtcars', 'iris')),
    uiOutput('columns')
  ),
  server = function(input, output){
    output$columns <- renderUI({
      mydata <- get(input$data)
      tagList(
      selectInput('columns2', 'Columns', names(mydata)),
      selectInput('columns3', 'Columns 2', names(mydata)))
    })
  }
))
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
0

Here is your working example with the code you provided and the output you asked :

## app.R ##
library(shiny)
library(shinydashboard)
library(DT)

#Declaring the UI
ui <- fluidPage(
  titlePanel("Interactive Menu"),

  # Create a new Row in the UI for selectInputs
    fluidRow(
      column(4,
      selectInput("names",
                  "Customer:",
                  c("A","B")))
      )
    ,
  fluidRow(
    conditionalPanel(
      condition = "input.names == 'A'",
      column(4,
             selectInput("names",
                         "Customer:",
                         c(as.character(iris$Species)))
             ),
      column(4,
             sliderInput("slide", "Select the slider one",
                         min = 75, max = 100,
                         value = 75, step = 5)),
      column(4,
             sliderInput("city", "Select the slider two",
                         min = 60, max = 100,
                         value = 60, step = 10)
      )
      ),
    conditionalPanel(
      condition = "input.names == 'B'",
    column(4,
           selectInput("names",
                       "Customer:",
                       c(as.character(iris$Species)))
           ))
    ),

  # Create a new row for the table.
  fluidRow(
    DT::dataTableOutput("table1")
  )
)
#Declaring the Server
server <- function(input, output) {

  # Filter data based on selections
  output$table1 <- renderDataTable({

    datatable(iris, options = list(
      searching = FALSE,
      pageLength = 5,
      lengthMenu = c(5, 10, 15, 20)
    ))
  })
}
shinyApp(ui, server)

enter image description here

enter image description here

As stated in the comments, the trick is to use conditionalPanel.

  • Please help with this link https://stackoverflow.com/questions/46783181/sliderinput-issue-with-table-in-r-shiny – Ashmin Kaul Oct 17 '17 at 06:14
  • please help me with this post https://stackoverflow.com/questions/47154749/changing-the-width-height-and-alignment-of-the-shiny-widgets-in-r?noredirect=1#comment81263002_47154749 – Ashmin Kaul Nov 07 '17 at 16:58