【问题标题】:How to dynamically update the range of x-axis of histogram using sliders in Shiny in R如何使用R中Shiny中的滑块动态更新直方图的x轴范围
【发布时间】:2020-07-16 00:17:56
【问题描述】:

我见过用户使用sliderInput调整直方图Bin数的例子。

但我的问题是如何使用 sliderInput 来调整 x 轴的范围而不是 bin 的数量?我应该包括什么代码?

谁能帮助我?我希望我的问题没有那么令人不安......

非常感谢。

【问题讨论】:

  • 到目前为止你尝试过什么?请发布您的代码。

标签: r user-interface shiny shinyapps


【解决方案1】:

这是一个基于可用 faithful 数据集的工作演示。我添加了一个sliderInput 来调整x 轴范围。 hist 包括 xlim 以定义 x 轴范围。注意第一个值是下限,第二个值是上限。

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)

【讨论】:

  • 非常感谢!真的很有帮助,真的非常感谢它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 2015-10-16
  • 1970-01-01
  • 2018-10-22
相关资源
最近更新 更多