rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.38k stars 1.86k forks source link

feature request: Option to use svg in in renderPlot #2057

Open vnijs opened 6 years ago

vnijs commented 6 years ago

Adding an argument to renderPlot for the device type to use, png or svg, would be very nice

RSchwinn commented 4 years ago

I'm also interested in this. Is anyone working on it? If not, I'd be happy to try, but may needs some tips on how to get started.

cpsievert commented 4 years ago

It's a bit of bolierplate, but just FYI, this is possible with a combination of renderImage() and the new htmltools::capturePlot() function:

library(shiny)

ui <- fluidPage(
  imageOutput("p")
)

renderSVG <- function(expr, width = 8, height = 3.75, ...) {
  file <- htmltools::capturePlot(
    expr, tempfile(fileext = ".svg"),
    grDevices::svg,
    width = width, height, ...
  )
  renderImage(list(src = file), deleteFile = TRUE)
}

server <- function(input, output, session) {
  output$p <- renderSVG({
    plot(pressure)
  })
}

shinyApp(ui, server)