R反应直方图 [英] R reactive histogram

查看:11
本文介绍了R反应直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习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')})
    })

是否有办法使其反应更快/实时?

推荐答案

感谢Dean的回复,我调查了gihub中的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")
    )
  )
))

应用程序现在随着您移动滑块而更新

这篇关于R反应直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆