Show slider inputs based on radio buttons in Shiny R

Viewed 353

How do I get the Slider (name it for example "Divide mtcars wt") to popup for "option b" radio button, and divide x in aes (which is wt from mtcars datasets) of "option b" with that Slider Value in the reproducible example below? And then if "option a" radio button is selected, hide that Slider again. I have looked at several Stack overflow posts but they didn't help. I was keep getting NULL plot output error for "option b" when I implemented it so i took it out.

library(shiny)
library(ggplot2)
options(warn=-1)

#ui.r
ui <- pageWithSidebar(
  headerPanel("many plots app"),

  sidebarPanel(
    sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100), 
    sliderInput("height", "Plot Height (px)", min = 0, max = 800, value = 800),
    uiOutput("filter_plot")

  ),

  mainPanel(
    uiOutput("plot")
  )
)
#server.r
server <- function(input, output, session) {

  options(warn = -1) #This doesn't stop console warning

  output$filter_plot<-renderUI({
    radioButtons("rd","Select Option",choices = c("a",
                                                  "b",
                 selected = "a"))
  })

output$plot <- renderUI({
    if(input$rd=="a"){
      output$plot1<- renderPlot({
        ggplot2::ggplot(mtcars, aes(mpg, cyl))+ geom_point()
      })
      plotOutput("plot1", width = paste0(input$width, "%"), height = input$height)
    }
    else if(input$rd=="b"){
      output$plot2<- renderPlot({
        ggplot2::ggplot(mtcars, aes(wt, qsec))+ geom_line()

      })
      plotOutput("plot2", width = paste0(input$width, "%"), height = input$height)
    }
  })
}
shinyApp(ui = ui, server = server)

Thank you in advance!

One more favour if you can, why are there warning in the console eventhough I have hidden them. My app works, but the warnings are there:

#Warning: Error in if: argument is of length zero
#  96: renderUI [C:/Users/YOU/Documents/DIRECTORY/xyz.R#443]
#  95: func
#  82: origRenderFunc
#  81: output$plot
#  1: runApp
1 Answers

You can use conditionalPanel to show/hide a sliderInput when 'b' is selected from your radiobutton input.

To prevent your warning, you can require req(input$rd) before using it in your if statement.

Let me know if this is the behavior you had in mind.

library(shiny)
library(ggplot2)
options(warn=-1)

#ui.r
ui <- pageWithSidebar(
  headerPanel("many plots app"),

  sidebarPanel(
    sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100), 
    sliderInput("height", "Plot Height (px)", min = 0, max = 800, value = 800),
    uiOutput("filter_plot"),
    conditionalPanel(
      "input.rd == 'b'",
      sliderInput("sl_wt", "Divide Weight", min = 1, max = 10, value = 5)
    )
  ),

  mainPanel(
    uiOutput("plot")
  )
)
#server.r
server <- function(input, output, session) {

  options(warn = -1) #This doesn't stop console warning to stop

  output$filter_plot<-renderUI({
    radioButtons("rd","Select Option",choices = c("a",
                                                  "b",
                                                  selected = "a"))
  })

  output$plot1<- renderPlot({
    ggplot2::ggplot(mtcars, aes(mpg, cyl))+ geom_point()
  })

  output$plot2<- renderPlot({
    ggplot2::ggplot(mtcars, aes(wt/input$sl_wt, qsec))+ geom_line()
  })

  output$plot <- renderUI({
    req(input$rd)
    if(input$rd=="a"){
      plotOutput("plot1", width = paste0(input$width, "%"), height = input$height)
    }
    else if(input$rd=="b"){
      plotOutput("plot2", width = paste0(input$width, "%"), height = input$height)
    }
  })
}
shinyApp(ui = ui, server = server)
Related