【问题标题】:Is it possible to initialize brush in ggplot in a shiny app?是否可以在闪亮的应用程序中初始化 ggplot 中的画笔?
【发布时间】:2016-07-13 00:09:33
【问题描述】:

我有一个shiny 应用程序,我想要一个ggplot 和一个brush,这样用户就不需要在每次应用程序启动时都选择某个感兴趣的区域。以后当然用户可以选择不同的区域。这是一个开始的例子:

library(shiny)
library(ggplot2)

runApp(shinyApp(
  ui = fluidPage(plotOutput('plotA', brush = brushOpts(id = 'plotA_brush')),
                 plotOutput('plotZ')),
  server = function(input, output, session) {
    pollData <- reactivePoll(60 * 1000, session,
                             checkFunc = function(){ Sys.time() },
                             valueFunc = function(){ data.frame(x = 1:100, y = cumsum(rnorm(100)))})
    output$plotA <- renderPlot({
      dt <- pollData()
      ggplot(dt, aes(x, y)) + geom_line()
    })
    ranges <- reactiveValues(x = NULL, y = NULL)
    observe({
      brush <- input$plotA_brush
      if(!is.null(brush)) {
        ranges$x <- c(brush$xmin, brush$xmax)
        ranges$y <- c(brush$ymin, brush$ymax)
      } else {
        ranges$x <- NULL
        ranges$y <- NULL
      }
    })
    output$plotZ <- renderPlot({
      dt <- pollData()
      ggplot(dt, aes(x, y)) + geom_line() + coord_cartesian(xlim = ranges$x, ylim = ranges$y)
    })
  }
))

【问题讨论】:

  • 把它初始化成什么?它开始“初始化”到整个区域。我怀疑您想将其初始化为特定部分,但是什么部分?还有什么比整个东西更小的吗?
  • 是的,任何比整体小的东西。在这个特定的示例中,假设我希望 x 在 25 到 75 的范围内,y 从最小值到最大值。

标签: r ggplot2 shiny


【解决方案1】:

是的,这是可能的。

在下面的代码中,我只添加了几行。首先,我添加了set.seed(42),以便图形可重现。其次,有一个dput(brush) 已被注释掉。这有助于确定我想要的初始画笔。最后,在控制rangesobserve 环境中,我添加了一个if else 设置为使用默认brush,它是来自input$plotA_brush 对象的NULL 值。

library(shiny)
library(ggplot2)
set.seed(42)

runApp(shinyApp(
  ui = fluidPage(plotOutput('plotA', brush = brushOpts(id = 'plotA_brush')),
                 plotOutput('plotZ')),
  server = function(input, output, session) {


    pollData <- reactivePoll(60 * 1000, session,
                             checkFunc = function(){ Sys.time() },
                             valueFunc = function(){ data.frame(x = 1:100, y = cumsum(rnorm(100)))})
    output$plotA <- renderPlot({
      dt <- pollData()
      ggplot(dt, aes(x, y)) + geom_line()
    })
    ranges <- reactiveValues(x = NULL, y = NULL)
    observe({
      if (is.null(input$plotA_brush)) {
        brush <- structure(list(xmin = 14.313925002001, xmax = 39.942241912585, ymin = 1.1077251080591, ymax = 5.5028180250535, mapping = structure(list( x = "x", y = "y"), .Names = c("x", "y")), domain = structure(list( left = -3.95, right = 104.95, bottom = -4.07771077213569, top = 9.69030145758825), .Names = c("left", "right", "bottom", "top")), range = structure(list(left = 32.3904099935947, right = 674.020527857828, bottom = 368.859578048224, top = 5.47945189149413), .Names = c("left", "right", "bottom", "top")), log = structure(list(x = NULL, y = NULL), .Names = c("x", "y")), direction = "xy", brushId = "plotA_brush", outputId = "plotA"), .Names = c("xmin", "xmax", "ymin", "ymax", "mapping", "domain", "range", "log", "direction", "brushId", "outputId"))
      } else {
        brush <- input$plotA_brush
      }
      # dput(brush)  # Useful for finding the initial brush
      if(!is.null(brush)) {
        ranges$x <- c(brush$xmin, brush$xmax)
        ranges$y <- c(brush$ymin, brush$ymax)
      } else {
        ranges$x <- NULL
        ranges$y <- NULL
      }
    })
    output$plotZ <- renderPlot({
      dt <- pollData()
      ggplot(dt, aes(x, y)) + geom_line() + coord_cartesian(xlim = ranges$x, ylim = ranges$y)
    })
  }
))

初始起始页面如下所示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2016-10-15
    • 2021-01-20
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多