Antonio
Antonio

Reputation: 1111

Remove Slider when selecting an option in Shiny

I would like help with the following problem:

The executable code below shows a panel of conditions when inserting an excel file (can be any). You have the options Yes or No. If the option No is requested, the following options appear: Change the filters or Change the cluster number, as you can see in the attachment. If I select the option to change the cluster number, a Slider is shown. However, if I press the Yes option the Slider should be removed, however it is not being done (attached). Therefore, I would like to adjust this.

library(shiny)
library(shinythemes)
library(readxl)

df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35), 
                   Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,  -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9), 
                   Longitude = c(-49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.7, -49.7, -49.7, -49.7, -49.7, -49.6, -49.6, -49.6, -49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6), 
                   Waste = c(526, 350, 526, 469, 285, 175, 175, 350, 350, 175, 350, 175, 175, 364, 175, 175, 350, 45.5, 54.6,350,350,350,350,350,350,350,350,350,350,350,350,350,350,350,350)), 
              class = "data.frame", row.names = c(NA, -35L))

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      fileInput("data", h3("Import excel")), 
                      sidebarLayout(
                        sidebarPanel(
                        
                          conditionalPanel(
                            "output.fileUploaded == true",
                            tags$hr(style="border-color: black;"),
                            tags$p(h3("Are you satisfied?")),
                            radioButtons( "satisfaction","", choices = list("Yes" = 1,"No " = 2),selected = 1)),
                          
                          conditionalPanel(
                            "input.satisfaction == '2'",
                            selectInput("naosatisf", h4("Choose a option below:"), 
                                        choices = c("no option selected" = "","Change the filters" = 1, "Change the cluster number" = 2), selected = "")),
                          
                          conditionalPanel(
                            "input.naosatisf == '2'",  
                            sliderInput("Slider", h5(""),
                                        min = 2, max = 31, value = "")),
                        
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                      ))))

server <- function(input, output, session) {
  
  v <- reactiveValues(df = NULL, clear = FALSE)
  
  observeEvent(input$data, {
    v$df <- read_excel(input$data$datapath)
    v$clear <- TRUE
  })
  
  output$fileUploaded <- reactive({
    v$clear
  })
  
  outputOptions(output, "fileUploaded", suspendWhenHidden = FALSE)
  
}

shinyApp(ui = ui, server = server)

enter image description here

enter image description here

Upvotes: 0

Views: 180

Answers (1)

Ben
Ben

Reputation: 30474

I just moved a parenthesis ) so that your third conditionalPanel is nested within your second one. Does this give you the desired behavior?

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      fileInput("data", h3("Import excel")), 
                      sidebarLayout(
                        sidebarPanel(
                          
                          conditionalPanel(
                            "output.fileUploaded == true",
                            tags$hr(style="border-color: black;"),
                            tags$p(h3("Are you satisfied?")),
                            radioButtons( "satisfaction","", choices = list("Yes" = 1,"No " = 2),selected = 1)),
                          
                          conditionalPanel(
                            "input.satisfaction == '2'",
                            selectInput("naosatisf", h4("Choose a option below:"), 
                                        choices = c("no option selected" = "","Change the filters" = 1, "Change the cluster number" = 2), selected = ""),
                          
                          conditionalPanel(
                            "input.naosatisf == '2'",  
                            sliderInput("Slider", h5(""),
                                        min = 2, max = 31, value = "")),
                          
                        )),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                      ))))

Upvotes: 1

Related Questions