Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

echarts4r and shiny downloadHandler #528

Closed
svenb78 opened this issue Jun 6, 2023 · 14 comments
Closed

echarts4r and shiny downloadHandler #528

svenb78 opened this issue Jun 6, 2023 · 14 comments
Assignees
Labels
feature New feature

Comments

@svenb78
Copy link

svenb78 commented Jun 6, 2023

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_to_PPTX export function. :-)

@rdatasculptor
Copy link
Contributor

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

@munoztd0 munoztd0 added the feature New feature label Jun 13, 2023
@munoztd0 munoztd0 self-assigned this Jun 13, 2023
@rdatasculptor
Copy link
Contributor

rdatasculptor commented Jun 17, 2023

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
Copy link
Owner

Echarts has a functionality for this, we can integrate.

@rdatasculptor
Copy link
Contributor

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

@JohnCoene
Copy link
Owner

Ah programmatically, missed that.

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

Maybe something to look at.

@svenb78
Copy link
Author

svenb78 commented Jun 20, 2023

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
Copy link
Owner

You probably get a white image because of the animation.

Set e_animation(show = FALSE)

@JohnCoene
Copy link
Owner

JohnCoene commented Jun 20, 2023

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
Copy link
Contributor

rdatasculptor commented Jun 21, 2023

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
Copy link
Collaborator

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
Copy link
Contributor

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

@svenb78
Copy link
Author

svenb78 commented Jun 22, 2023

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

@svenb78
Copy link
Author

svenb78 commented Jun 23, 2023

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

I will close the issue.

@svenb78 svenb78 closed this as completed Jun 23, 2023
@JohnCoene
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

No branches or pull requests

4 participants