0

I'm trying to implement zoom-in feature in plot (usual ggplot2 and base R plot) used in shiny renderplot module. Here is the code.

ui <- basicPage(
  plotOutput("plot1",
             brush = "plot_brush"
  )
)

server <- function(input, output) {
  output$plot1 <- renderPlot({

    plot(mtcars$wt, mtcars$mpg, xlim = c(input$plot_brush$xmin, input$plot_brush$xmax), ylim=c(input$plot_brush$ymin, input$plot_brush$ymax))

  })
}

shinyApp(ui, server)

Here I want to set the input : - input$plot_brush to NULL after the zoom is applied. I know this will be requiring modification in the renderPlot block of the code but apart from that how can I set input$plot_brush to NULL [which will make the selection disappear from the plot]. I know update input functions are there in order to update UI [like shiny::updateSelectInput() etc] but those can't be used here for this purpose. Right now only one way to solve this is to have a base plot and a zoomed plot where zoom will be applied after selecting a region on base plot [as implemented in https://gallery.shinyapps.io/105-plot-interaction-zoom/].

Please help me on this.

1 Answer 1

2

If I understood you correctly setting the brush to NULL is just to remove the blue brush selection?

If so you can do it like this:

ui <- basicPage(
  plotOutput("plot1",
             brush = brushOpts("plot_brush",resetOnNew=T),
             dblclick = 'dblclick'
  )
)

#
# Zoom in on plot brush, restore zoom on dbl click
#
server <- function(input, output) {
  ranges <- reactiveValues(x=NULL, y=NULL)

  output$plot1 <- renderPlot({
    plot(mtcars$wt, mtcars$mpg, xlim = ranges$x, ylim=ranges$y)
  })
  # Set limits for zoom
  observe({
    brush <- input$plot_brush
    if (!is.null(brush)) { ranges$x <- c(brush$xmin, brush$xmax); ranges$y <- c(brush$ymin, brush$ymax); } 
  })
  # Zoom out on doubleclick
  observeEvent(input$dblclick,{
    ranges$x <- NULL; ranges$y <- NULL; 
  })
}
#
# Zoom in on dbl click, restore zoom on dbl click
#
server2 <- function(input, output) {
  ranges <- reactiveValues(x=NULL, y=NULL)

  output$plot1 <- renderPlot({
    plot(mtcars$wt, mtcars$mpg, xlim = ranges$x, ylim=ranges$y)
  })

  # Set ranges for plot zoom
  observeEvent(input$dblclick,{
    brush <- input$plot_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; }
  })
}
shinyApp(ui, server)

Here I'm using reset on new to remove the brush selection after zoom.

Server shows how you can zoom in by simply brushing (dbl click to zoom out) and

Server2 shows how you can zoom in by doubleclicking on the brushed area to zoom in, dbl clicking with no brush zooms out.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.