微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

下载 pdf 格式的瀑布图在 Shinydashboard 中不起作用

如何解决下载 pdf 格式的瀑布图在 Shinydashboard 中不起作用

我正在尝试将在闪亮仪表板中创建的以下瀑布图下载为 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","total")
    text= c("+60","+80","","-40","-20","Total")
    y= c(60,80,-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)

解决方法

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

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

使用 plotly::export 捕获

注意:export 正在被弃用(请改用 orca)。 orca 需要安装一些 extra system dependencies

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。