Home:ALL Converter>Shiny Dashboard Plot Zoom

Shiny Dashboard Plot Zoom

Ask Time:2019-09-05T17:45:58         Author:lb0389

Json Formatter

I am new to shiny dashboards. I created a very simple scatter plot that I want overlayed to a png image. I need a way to zoom in and out of the scatter plot AND the image.

Currently, the zoom in and out works for the geom_points, but not for the image.

I patterned my code to this tutorial:

https://shiny.rstudio.com/gallery/plot-interaction-zoom.html

library(ggplot2)





if (!requireNamespace("BiocManager", quietly = TRUE))
   install.packages("BiocManager")

   BiocManager::install("EBImage")
   library("EBImage")

   x <- readImage("U:/Sample/floor plan sample.png")

   # Scale to a specific width and height
   LayoutJPG <- resize(x, w = 500, h = 500)
   display(LayoutJPG)


   ui <- fluidPage(
     fluidRow(
       column(width = 12, 
              plotOutput("plot1", height = 300,
                  dblclick = "plot1_dblclick",
                  brush = brushOpts(
                    id = "plot1_brush",
                    resetOnNew = TRUE
                  )
              )
       )

     )
   )

   server <- function(input, output) {

     ranges <- reactiveValues(x = NULL, y = NULL)

     output$plot1 <- renderPlot({
       ggplot(mtcars, aes(wt, mpg)) +
         annotation_custom(grid::rasterGrob( LayoutJPG ,
                                      width = unit(1,"npc"),
                                      height = unit(1,"npc")),
                    -Inf, Inf, -Inf, Inf) +

  geom_point() +
  coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)
     })

     # When a double-click happens, check if there's a brush on the plot.
     # If so, zoom to the brush bounds; if not, reset the zoom.
     observeEvent(input$plot1_dblclick, {
       brush <- input$plot1_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)

As mentioned above, currently, the zoom in and out works for the geom_points, but not for the image.

Author:lb0389,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57802722/shiny-dashboard-plot-zoom
yy