0

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)
TarJae
  • 72,363
  • 6
  • 19
  • 66

1 Answers1

1
  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)
Thomas
  • 1,252
  • 6
  • 24
  • 1
    Thank you Thomas. After trying your solution I ran again in an error message: "for best results with ggplot2 and lattice, please install gridSVG Warnung: Error in : '' does not exist in current working directory ('F:/R_Project/...)". Then I installed gridSVG and put library(gridSVG) and things worked. Thank you again. Step by step I am getting into it. – TarJae Dec 09 '20 at 16:47
  • Thanks for noticing, changed my answer accordingly – Thomas Dec 10 '20 at 08:33