【问题标题】:R reactive histogramR反应直方图
【发布时间】:2016-05-24 17:42:38
【问题描述】:

我正在学习RShiny 接口并完成第一个示例。

直方图根据滑块输入而变化。当滑块移动时,如何让它不断更新?现在,它仅在滑块停止移动时更新。

ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

server.R

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  # Expression that generates a histogram. The expression is
  # wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should re-execute automatically
  #     when inputs change
  #  2) Its output type is a plot

  output$distPlot <- renderPlot({
    x    <- faithful[, 2]  # Old Faithful Geyser data
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
})

我尝试过的:

#server.R
    library(shiny)

    shinyServer(function(input,output) {
                    x<-faithful[,2]
                    bins <- reactive({
                            seq(min(x),max(x),length.out=input$bins+1)
                    })

                    output$distPlot <- renderPlot({
                    hist(x,breaks=bins(),col='darkgray',border='white')})
    })

有没有办法让它反应更快/实时?

【问题讨论】:

  • 我想知道幕后是否有一些 javascript 说“必须在我们更新 input$bins 之前停止特定值”,这将是一个有趣的问题,可以问 RStudio 人员(或那些真的知道闪亮)。如果是这种情况,那么加速您的代码将无济于事,但如果可能的话,需要注入一些新的 javascript。不是解决方案,但现在我很好奇。
  • 这可能是有原因的,虽然这个简单的直方图可能很快,但某些绘图代码可能很慢,并且动态重绘图形是不明智的(它可能会崩溃服务器)。正如@AndrewTaylor 所说,必须是Shiny 中的代码来控制这种行为。如果您想要实时更新的直方图,您可能想尝试一些纯 Javascript 解决方案而不是 Shiny,因为 Shiny 需要重新绘制整个图表,但 Javascript 解决方案可能只需要更新更改的内容。
  • 看来 Shiny 使用 Ion.RangeSlider javascript 库来创建实际的滑块。如果您阅读页面ionden.com/a/plugins/ion.rangeSlider/en.html,您会看到javascript 库同时具有onChangeonFinish 事件,前者会在值更改时调用代码,但后者仅在用户释放鼠标时才会调用代码。 Shiny 似乎使用了后一种选项,但没有面向用户的选项来改变这种行为。
  • 不幸的是,我还没有足够的 Javascript 知识。谢谢你们的cmets!
  • 来自 Shiny 的有用资源:shiny.rstudio.com/articles/building-inputs.html 另外,Homer White 有一个可能对您有所帮助的软件包,它有一个我从未尝试过的 customSliderInput() 功能,但如果我没记错的话,它可以让您自定义费率输入更新的位置github.com/homerhanumat/shinyCustom

标签: r shiny


【解决方案1】:

感谢 Dean 的回复,我调查了 github 中的 shinyCustom 包。

这是我的解决方案:

#Shiny UI
library(shiny)
library(shinyCustom)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      useShinyCustom(slider_delay = "0"), #make a call to shinyCustom, where there is no delay
      customSliderInput("bins", #Use customSliderInput instead of sliderInput
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

当您移动滑块时,应用程序现在会更新

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2013-06-17
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多