Last active
August 29, 2015 14:11
-
-
Save geotheory/d8d4a14542c0b9d32786 to your computer and use it in GitHub Desktop.
Output (non-ggplot) Shiny plots to PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
shinyServer( | |
function(input, output) { | |
plotInput <- reactive({ | |
if(input$returnpdf){ | |
pdf("plot.pdf", width=as.numeric(input$w), height=as.numeric(input$h)) | |
plot(rnorm(sample(100:1000,1))) | |
dev.off() | |
} | |
plot(rnorm(sample(100:1000,1))) | |
}) | |
output$myplot <- renderPlot({ plotInput() }) | |
output$pdflink <- downloadHandler( | |
filename <- "myplot.pdf", | |
content <- function(file) { | |
file.copy("plot.pdf", file) | |
} | |
) | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(shiny) | |
pageWithSidebar( | |
headerPanel("Output to PDF"), | |
sidebarPanel( | |
checkboxInput('returnpdf', 'output pdf?', FALSE), | |
conditionalPanel( | |
condition = "input.returnpdf == true", | |
strong("PDF size (inches):"), | |
sliderInput(inputId="w", label = "width:", min=3, max=20, value=8, width=100, ticks=F), | |
sliderInput(inputId="h", label = "height:", min=3, max=20, value=6, width=100, ticks=F), | |
br(), | |
downloadLink('pdflink') | |
) | |
), | |
mainPanel({ mainPanel(plotOutput("myplot")) }) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment