Cannot download plotly charts into png format or svg format.

I am using the latest version of R, plotly and shiny. I have used the orcaa command to save the plotly images in the past and has worked for me. However, when trying to save the plotly charts in shiny using orca does not seem to be working.

The best I could do is either save the chart and no display or display the chart and not get it saved using the orca function. Is there a fix to this. I am using Shiny modules in my code.

Any help is appreciated. The part of the code is as follows (with some comments in it).



plotting_module_UI <- function(id) {
  message("Plotting the charts in progress now")
  
   plotlyOutput(ns("s"), height = "750px", width = "1800px"),
    
    fluidRow(
      column(
        2,
        downloadButton(outputId = ns("downloadchartdatabut"), label = "Download raw data set for this chart")) # button to download the raw data in a csv file
    )
  )
  
}
plotting_module <- function(input, output, session, cn){
 ###    some more data manipulation and data massaging
  chart <- reactive({
      plot_ly(newdata, x = eval(parse(text = paste("newdata$",input$s_xaxis,sep = ""))), y = newdata$fValue, height = 720, width = 1200, type = "box", 
              color = eval(parse(text = paste("newdata$",input$s_groups,sep = ""))), pointpos = 0, boxpoints = 'all') %>% 
        layout(yaxis = c(list(title = paste(unique(newdata$vcStructure)," (",unique(newdata$vcUnits),") ", sep = "" ), range = range_ip()), a1), 
               margin = list(l = 100)) %>% 
        layout(boxmode = "group") %>% 
        layout(xaxis = c(list(title = input$s_xaxis, tickangle = 90), a1)) %>%
        layout(shapes = list(hline(unique(newdata$fLowerSpec)),hline(unique(newdata$fUpperSpec)))) %>%
        layout(title = paste(newdata$vcVendor, newdata$vcProcess, newdata$vcStructure, sep = " / "))
        
       # orca(chart(), paste(unique(newdata$vcStructure)[1],".png", sep = ""))    # orca function under the reactive function does not show any output but saves the chart directly which is not helpful as we need to change the charts based on some buttons on the shiny page.
    }
  })
    output$s <- renderPlotly(chart())
  reactive({orca(chart(), paste(unique(newdata$vcStructure)[1],".png", sep = ""))})  # without the reactive function, R gives and error that function like this should be in reactive environment. However, even with reactive, no image is being saved.
}

app = shinyApp(
  ui = fluidPage(
#### display of some tables and buttons
  fluidRow(
      lapply(1:length(unique(getData$vcStructure)), function(i) plotting_module_UI(unique(getData$vcStructure)[i])))
  ),
  
  server = function(input, output, session) {
  #####  some more data code for display of tables etc.
    lapply(1:5,
           function(i) 
             callModule(plotting_module, unique(getData$vcStructure)[i], unique(getData$vcStructure)[i]))
    
    session$onSessionEnded(function() {
      stopApp()
    })
 })

1 Like

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

just updated the comment before.

Your code gives me an error -- here's a minimally reproducible example of exporting a plotly graph via orca in shiny:

library(shiny)

ui <- fluidPage(
  actionButton("export", "Export"),
  plotlyOutput("p")
)

server <- function(input, output, session) {
  
  myPlotlyGraph <- reactive({
    ggplotly(qplot(1:10))
  })
  
  observeEvent(input$export, {
    orca(myPlotlyGraph())
  })
  
  output$p <- renderPlotly({
    myPlotlyGraph()
  })
  
}

shinyApp(ui, server)

Just to clarify -- have you ever been able to run orca() successfully in a shiny app? Assuming you can run orca() successfully outside of shiny, then it should work inside shiny as well (when shiny is running on that same machine). Note, however, that services like shinyapps.io and connect don't currently have the system libraries required to run orca() -- hopefully that story changes in the near future.

2 Likes

Thanks for the answer. I am wondering if by default i want to download the image, i have to remove the observeEvent function and put the orca command into a reactive function, right? Putting the orca command into a reactive function does not work for me.

So you just want to export the plot as it appears on initial page load (without any user intervention)?

Yes. Exactly. That's what I want to do. And I believe that as I change my plot, the new plot will over ride the previous file.

I am now plotting the charts using ggplot and using ggsave to save the plots but it is still not working. Am i missing something or is it that once i display the chart, i cannot save it in the working directory..

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.