Skip to content

Instantly share code, notes, and snippets.

@jcheng5

jcheng5/app.R Secret

Last active May 25, 2016 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcheng5/c9b55ad32bb4b258cf7073574f6addce to your computer and use it in GitHub Desktop.
Save jcheng5/c9b55ad32bb4b258cf7073574f6addce to your computer and use it in GitHub Desktop.
library(shiny)
zoomPlotOutput <- function(id, ..., zoomLabel = "Zoom") {
ns <- NS(id)
tagList(
plotOutput(ns("plot"), ...),
downloadButton(ns("zoom"), zoomLabel)
)
}
zoomPlot <- function(input, output, session, width, height, plotFunc) {
output$plot <- renderPlot({
plotFunc()
})
url <- session$registerDataObj(session$ns("zoom"), NULL, function(data, req) {
path <- shiny::plotPNG(plotFunc, width = width, height = height)
on.exit(unlink(path))
list(
status = 200L,
headers = list("Content-Type" = "image/png"),
body = readBin(path, raw(), file.size(path))
)
})
output$zoom <- function() { url }
}
ui <- fluidPage(
zoomPlotOutput("cars")
)
server <- function(input, output, session) {
callModule(zoomPlot, "cars", 800, 600, function() {
plot(cars)
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment