0

Im trying to download the following waterfall chart created inside a shiny dashboard as a pdf but when I use the shinydashboard interface I download an empty pdf.

library(shiny)
library(plotly)
library(shinydashboard)
library(shinydashboardPlus)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    downloadButton('downloadPlot', 'Download Plot')
  ),
  dashboardBody(plotlyOutput('pl'))
)


server <- function(input, output, session) {
  
  plotInput <- function(){
    x= list("Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax")
    measure= c("relative", "relative", "total", "relative", "relative", "total")
    text= c("+60", "+80", "", "-40", "-20", "Total")
    y= c(60, 80, 0, -40, -20, 0)
    data = data.frame(x=factor(x,levels=x),measure,text,y)
    
    fig <- plot_ly(
      data, name = "20", type = "waterfall", measure = ~measure,
      x = ~x, textposition = "outside", y= ~y, text =~text,
      connector = list(line = list(color= "rgb(63, 63, 63)"))) 
    fig <- fig %>%
      layout(title = "Profit and loss statement 2018",
             xaxis = list(title = ""),
             yaxis = list(title = ""),
             autosize = TRUE,
             showlegend = TRUE,waterfallgap = "0.8")
    
    fig
  }
      
  
  
      output$pl<-renderPlotly({
        plotInput()
      })

      output$downloadPlot <- downloadHandler(
        filename = "Shinyplot.pdf",
        content = function(file) {
          pdf(file)
          plotInput()
          dev.off()
        })  
  
}

shinyApp(ui = ui, server = server)
firmo23
  • 7,490
  • 2
  • 38
  • 114

1 Answers1

1

Plotly is not standard R graphics. You can't use dev to capture it. Try this:

    output$downloadPlot <- downloadHandler(
        filename = "Shinyplot.pdf",
        content = function(file) {
            plotly::export(plotInput(), file)
    }) 

Use plotly::export to capture

Note: export is in the process of being deprecated (use orca instead). orca needs to install some extra system dependencies.

Usage is similar: orca(plotInput(), file = file)

lz100
  • 6,990
  • 6
  • 29
  • How can I force using plotly::export if I dont weant to install orca – firmo23 Apr 02 '21 at 21:54
  • Just use `plotly::export` then. It is working. The message is just to let you know it will not work one day and you should prepare to switch to `plotly::orca` – lz100 Apr 02 '21 at 22:28
  • i get an empty white pdf – firmo23 Apr 02 '21 at 22:53
  • I don't know your system information, `plotly::export` worked fine on my end and it should work on other machines as well. Maybe you are missing something, I can't tell – lz100 Apr 02 '21 at 23:56
  • in rstudio cloud it does not work as well – firmo23 Apr 03 '21 at 00:19
  • 1
    I just tried in Rstudio, no problem at all. It has to be your browser problem. Make sure you use the latest browser, like Chrome or Firefox, turn off some browser extension that will block javascripts – lz100 Apr 03 '21 at 00:36
  • I dont know what can block javascripts but it does not work in both of them – firmo23 Apr 03 '21 at 01:02