Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. library(shiny)
  2. library(ggplot2)
  3.  
  4. ui <- fluidPage(
  5. plotOutput("mainPlot",
  6. height = "600px",
  7. dblclick = "mainPlotDblclick",
  8. brush = brushOpts(id = "mainPlotBrush", resetOnNew = TRUE)
  9. )
  10. )
  11.  
  12. server <- function(input, output) {
  13.  
  14. data <- data.frame(x = rnorm(500), y = rnorm(500))
  15. zoom <- reactiveValues(xRange = NULL, yRange = NULL)
  16.  
  17. output$mainPlot <- renderPlot({
  18. p <- ggplot(data, aes(x, y)) + geom_point()
  19. if (is.null(zoom$xRange))
  20. p <- p + coord_fixed()
  21. else
  22. p <- p + coord_fixed(xlim = zoom$xRange, ylim = zoom$yRange)
  23. p
  24. })
  25.  
  26. observeEvent(input$mainPlotDblclick, {
  27. brush <- input$mainPlotBrush
  28. if (!is.null(brush)) {
  29. zoom$xRange <- c(brush$xmin, brush$xmax)
  30. zoom$yRange <- c(brush$ymin, brush$ymax)
  31. } else {
  32. zoom$xRange <- NULL
  33. zoom$yRange <- NULL
  34. }
  35. })
  36. }
  37.  
  38. shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement