【发布时间】: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从最小值到最大值。