plotly / plotly/plotly.R

Download images in Shiny

Open
#2,141 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

Hi! I'm using plotly in a shiny. When I want to download the image, first I used export, it works, but it returns a message "use orca", but it was also superseded by kaleido(). So I use save_image(), and it return an error .

Warning: Error in py_run_string_impl:   File "<string>", line 1
    open('C:\Users\limbo\AppData\Local\Temp\RtmpkD8Zlu\file1d5837d66529.png', 'wb').write(scope_1d58780818d2.transform(sys.modules['json'].loads(scope_1d58780818d2._last_plot), format='png', width=None, height=None, scale=None))
                                                                            ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

  [No stack trace available]

my shiny

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("plot"),
  downloadButton("download", "Download")
)

server <- function(input, output, session) {
  p <- reactive({
    
    a <- ggplot(iris, 
                aes(x = Species, 
                    y = Sepal.Length)
                ) + 
      geom_boxplot() + 
      theme_bw()
    a <- ggplotly(a)
    a
  })

  
  output$plot <- renderPlotly(p())
  
  output$download <- downloadHandler(
    filename = function(){
      paste0(paste0("test", 
                    Sys.Date()), 
             ".png")
    },
    
    content = function(file){
      plotly::save_image(p(), file = file)
    }
  )
}

shinyApp(ui, server)

What can I do about that? Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Shiny example on Windows, focusing on downloadHandler() and plotly::save_image() with kaleido. Trace the generated Python call and compare it with the temporary path shown in the error; done means the example downloads the PNG without the unicodeescape error.

Written by the indexing model from the issue text.

Assessment

Tech stack
plotly, python, r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.