Search code examples
rshinysvgpanzoomsvglite

svglite, svgPanZoom not able to plot in R shiny


According to the post How to display scatter plot with R Packages:svgPanZoom? I tried to replicate a zoomable plot in R shiny. Can someone help me with my code? Why can't I reproduce this code?

library(shiny)
library(svglite)
library(svgPanZoom)


# Define UI ----
ui <- shinyUI(bootstrapPage(
    # App title ----
  headerPanel("Cyl vtree"),
  
    
 # Main panel for displaying outputs ----
  svgPanZoom(
    svglite:::inlineSVG(
      show(p)
    ),
    controlIconsEnabled = T
  )
  
))

# Define server logic to plot ----
server <- function(input, output) {
  output$main_plot <- renderSvgPanZoom({
    p <- ggplot(mtcars, aes(x = cyl, y = mpg)) + geom_point()
    svgPanZoom(p, controlIconsEnabled = T)
  })
}

shinyApp(ui, server)

Solution

    1. svgPanZoomOutput is missing in your UI to bind svgpanzoom to shiny
    2. You have used svgPanZoom in UI which only belongs to renderSvgPanZoom in server
    3. It works in either way - using this solution or just the basic example from ?svgPanZoom
    library(shiny)
    library(svglite)
    library(svgPanZoom)
    library(ggplot2)
    library(gridSVG)
    
    # Define UI ----
    ui <- shinyUI(bootstrapPage(
      # App title ----
      headerPanel("Cyl vtree"),
      
      
      # Main panel for displaying outputs ----
      svgPanZoomOutput(outputId = "main_plot")
      
      
    ))
    
    # Define server logic to plot ----
    server <- function(input, output) {
      output$main_plot <- renderSvgPanZoom({
        p <- ggplot(mtcars, aes(x = cyl, y = mpg)) + geom_point()
    
        svgPanZoom(p, controlIconsEnabled = T)
      })
    }
    
    shinyApp(ui, server)