Skip to content

Instantly share code, notes, and snippets.

@geotheory
Created December 2, 2014 17:12
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 geotheory/3ed9e160472d4b330710 to your computer and use it in GitHub Desktop.
Save geotheory/3ed9e160472d4b330710 to your computer and use it in GitHub Desktop.
Shiny PDF output problem
require(shiny)
library(ggplot2)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
plotInput <- reactive({
df <- datasetInput()
plot(df[[1]], df[[2]])
})
output$plot <- renderPlot({
print(plotInput())
})
output$downloadPlot <- downloadHandler(
filename = function() { paste(input$dataset, '.pdf', sep='') },
content = function(file) {
pdf(file, width=8, height=6)
plotInput()
dev.off()
})
})
shinyUI(pageWithSidebar(
headerPanel('Downloading Data'),
sidebarPanel(
selectInput("dataset", "Choose a dataset:",
choices = c("rock", "pressure", "cars")),
downloadButton('downloadPlot', 'Download Plot')
),
mainPanel(
plotOutput('plot')
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment