0

我试图将在闪亮的仪表板中创建的以下瀑布图下载为 pdf,但是当我使用该shinydashboard界面时,我下载了一个空的 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)
4

1 回答 1

1

Plotly 不是标准的 R 图形。您不能使用 dev 来捕获它。尝试这个:

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

用于plotly::export捕获

注意:export正在被弃用(orca改为使用)。orca需要安装一些额外的系统依赖

用法类似:orca(plotInput(), file = file)

于 2021-04-02T17:40:07.100 回答