help to display an histogram from a select input

Hi
I try to change the histogram x axis from the choice done in my select input but I dont succeed

Could you help please?

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Histogramme dynamique"),
  
  
  sidebarPanel(
    selectInput('x', 'Liste déroulante', vars),
  
  ),
  
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  output$distPlot <- renderPlot({input$x
   

    # draw the histogram with the specified number of bins
    hist(x, col='blue', axes = TRUE, plot = TRUE)
  })
}
shinyApp(ui, server)

Your code is not reproducible as you provided no data.
Neither vars nor x

You do mention input$x in your render function but you dont use it for anything. So it has no effect

I assume you refer to the lovely mtcars dataset.
You need to filter your dataset, so only use the column from the input, done here with
x[[input$Selected_Cat]] - please note I changed the inputID to avoid confusion.

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Histogramme dynamique"),
  
  
  sidebarPanel(
    selectInput("Selected_Cat", 'Liste déroulante', vars),
    ),
  
  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("distPlot")
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  # define the inputs globally
  x = mtcars
  vars = colnames(mtcars)
  
  # make plot
  output$distPlot <- renderPlot({

  # draw the histogram with the specified category
    hist(x[[input$Selected_Cat]], col='blue', axes = TRUE, plot = TRUE)
  })
}
shinyApp(ui, server)

perfect, many thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.