download PDF/PNG format plot from app using downloadHandler

969 views
Skip to first unread message

carrie....@gmail.com

unread,
Sep 23, 2015, 4:27:17 PM9/23/15
to Shiny - Web Framework for R
Hi,
Here is my code. When I hit "download the plot", I got the massage showed in attachment.
Could anyone help to figure out what's wrong with it?
Thanks a lot!!

shinyServer(function(input,output){
  
  datasetInput <- reactive({
    switch(input$dataset,
           "Car model" = mtcars,
           "Car sth" = attitude)
  })
    
  mymtc <- reactive({
    dataset <- datasetInput()
    d3heatmap(dataset, colors ="Blues", scale = "col", dendrogram = "none")
  })
  
  output$mtc<-renderD3heatmap(mymtc())
  
  output$wrt<-renderDygraph({
    
    dygraph(weather, main = "Weather: Rainfall and Temperature") %>%
      dyRangeSelector(height = 60) %>%
      dyAxis("y", label = "Temperature (C)",independentTicks = TRUE) %>%
      dyAxis("y2", label = "Rainfall") %>%
      dySeries("rainfall", axis = 'y2')
  })
  
  output$down <- downloadHandler(
    
    filename = function() {
      paste("myreport", input$report, sep = ".")
    },
    content = function(file){
  
      if(input$report == "png")
        png(file)
      else
        pdf(file)
      mymtc()
      dev.off()
    }
  )
})

shinyUI(fluidPage(
  titlePanel("Motor Trend Car Road Tests"),
  
  sidebarLayout(
    sidebarPanel(
      helpText("The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973-74 models)."),
      radioButtons("dataset",
                   label = "Choose a variable to display",
                   choices = c("Car model","Car sth"),
                   selected = "Car model"),
      radioButtons("report",label = "Select a file type:", choices = list("png","pdf"))
    ),
    
    mainPanel(d3heatmapOutput("mtc", width = "1200px", height = "500px"),
              downloadButton("down", label = "Download the plot"),
              dygraphOutput("wrt",width = "1100px", height = "400px"))
  
)
)
)

Capture.PNG

carrie....@gmail.com

unread,
Sep 24, 2015, 10:13:52 AM9/24/15
to Shiny - Web Framework for R
Anyone can help?

Joe Cheng

unread,
Sep 24, 2015, 10:35:05 AM9/24/15
to carrie....@gmail.com, Shiny - Web Framework for R
Can you put a browser() call after dev.off, and from the browser prompt do list.files() and see if anything unexpected is in that directory?
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/d0a75fc0-4ed2-4bd6-ae61-9cb08e4d9a95%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

carrie....@gmail.com

unread,
Sep 24, 2015, 6:34:09 PM9/24/15
to Shiny - Web Framework for R, carrie....@gmail.com
Hi Joe,
Thank you so much for answering my question!
I've tried but nothing happen there.
Attached is my code. Could you help to have a look?
Thanks again!
ui.R
server.R
Reply all
Reply to author
Forward
0 new messages