我有一个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)
})
}
))