JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
587 stars 82 forks source link

echarts4r and shiny downloadHandler #528

Closed svenb78 closed 1 year ago

svenb78 commented 1 year ago

Hi,

some time ago, I made a shiny app which allows users to export plots -- made with ggplot2 -- into a Word document. I would like to use echarts graphs instead of ggplot, but I don't find a way to export them using a downloadHandler (--> check boxes). I found the e_toolbox_feature and saveAsImage argument, but that is not what I'm looking for.

Can someone help, e.g. with a minimal example for an _echarts4r_toPPTX export function. :-)

rdatasculptor commented 1 year ago

Have you tried webshot? Take a look here for some inspiration https://gist.github.com/dantonnoriega/0744e7537076128ac4a026b8b7f38f09

rdatasculptor commented 1 year ago

I tried webshot myself (since I had a use case as well) but no luck. So if we find a way to export echarts4r plots as an image (e.g. png), this would be most welcome @munoztd0! ,🙂

JohnCoene commented 1 year ago

Echarts has a functionality for this, we can integrate.

rdatasculptor commented 1 year ago

Export as images programmatically? (I am aware of the save image feature). This would be brillant!

JohnCoene commented 1 year ago

Ah programmatically, missed that.

Rmarkdown does it, when you use an htmlwidget in a document rendered to PDF.

Maybe something to look at.

svenb78 commented 1 year ago

Thanks for the replies!

@rdatasculptor : Using webshot, I get a white 'image' without any graphic. webshot2 needs Chrome, which I don't have available within the respective environment.

Unfortunately, my 'stakeholders' need a Shiny solution. So, an echarts4r add-on would be great! :-)

Btw.: The echarts4r library really is great!

JohnCoene commented 1 year ago

You probably get a white image because of the animation.

Set e_animation(show = FALSE)

JohnCoene commented 1 year ago

This works.

library(shiny)
library(echarts4r)

ui <- fluidPage(
  tags$head(
    tags$script(
      HTML("$(() => {
        $('#img').on('click', (e) => {
          let el = get_e_charts('plot');
          let img = el.getDataURL({pixelRatio: 2, backgroundColor: '#fff'});
          Shiny.setInputValue('imgstr', img)
        })
      });")
    )
  ),
  tags$a("Save image", id = "img", class = "btn btn-info"),
  echarts4rOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderEcharts4r({
    mtcars |>
      e_charts(mpg) |>
      e_scatter(wt, qsec)
  }) 

  observeEvent(input$imgstr, {
    str <- input$imgstr
    raw <- base64enc::base64decode(what = substr(str, 23, nchar(str)))
    png::writePNG(png::readPNG(raw), "plot.png")
  })
}

shinyApp(ui, server)
rdatasculptor commented 1 year ago

and this doesn't work,:

library(echarts4r)
plot <- mtcars |>
  e_charts(mpg) |>
  e_scatter(wt, qsec) |>
  e_animation(show = FALSE)

htmlwidgets::saveWidget(plot, "plot.html")
webshot::webshot("plot.html", "plot.png")

Again the result is a white image.

Or do I miss something here?

(@JohnCoene was this what you meant by e_animation(show = FALSE))?

munoztd0 commented 1 year ago

Yeah it's an issue form webshot see here

Works well with webshot2 though, @svenb78 is it really impossible to get chrome from your environment ?

rdatasculptor commented 1 year ago

webshot2 works! At least for me it does. if it works for you as well @svenb78, then we can close this issue.

svenb78 commented 1 year ago

@rdatasculptor : I will check and talk to my admin.

svenb78 commented 1 year ago

John's suggestion works and fits my needs. Many thanks for the help!

I will close the issue.

JohnCoene commented 1 year ago

We should probably document that somewhere, or even provide a wrapper around that download functionality