Here is a working demo based on the available faithful
dataset. I added a sliderInput
to adjust the x-axis range. The hist
includes xlim
to define the x-axis range. Note the first value is the lower limit, and the second value is the upper limit.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
sliderInput("x_range", "Range:",
min = 0, max = 100, value = c(0, 100), step = 10)
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output, session) {
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, xlim = c(input$x_range[1], input$x_range[2]), col = 'darkgray', border = 'white')
})
}
shinyApp(ui, server)