【问题标题】:Downlaod plotly waterfall chart as pdf does not work within shinydashboard以pdf格式下载plotly瀑布图在shinydashboard中不起作用
【发布时间】:2021-04-02 13:27:11
【问题描述】:

我正在尝试将以下在闪亮仪表板中创建的瀑布图下载为 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)

【问题讨论】:

    标签: r shiny download shinydashboard shinydashboardplus


    【解决方案1】:

    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)

    【讨论】:

    • 如果我不想安装 orca,如何强制使用 plotly::export
    • 然后使用plotly::export。这是工作。该消息只是为了让您知道它有一天不会起作用,您应该准备切换到plotly::orca
    • 我得到一个空白的 pdf 文件
    • 我不知道你的系统信息,plotly::export 在我这边工作得很好,它也应该在其他机器上工作。也许你错过了什么,我说不出来
    • 我刚在Rstudio里试过,完全没问题。应该是你的浏览器问题。确保您使用最新的浏览器,例如 Chrome 或 Firefox,关闭一些会阻止 javascripts 的浏览器扩展
    猜你喜欢
    • 2019-04-14
    • 2018-11-14
    • 2011-08-26
    • 2016-01-25
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多