简体   繁体   中英

How to add a download button for html format on a plotly modebar in R shiny

it is a newby question I'm making a Shiny app in R and I use Plotly to make my plots interactive.

On the modebar of plotly, there is a button to download on png, and we can change parameter to download the plot on svg or jpg. But I would like to add a button to export it with.html format.

I know we can add buttons, as:


    p %>%
config(
modeBarButtonsToAdd = list(dl_button))
and then :
    dl_button <- list(
name = "Download data",
icon = list(
path = icon_camera),
click = htmlwidgets::JS('function(gd) { }') ## 
But I'm stuck at this stage. I don't know what code to do now. 

I try to include htmlwidgets::saveWidget inside the function(gd) without success.

I don't know if it is possible as it. I have already done a downloadHandler() which is work well. but I would like directly such button on the modebar. Have you any idea?

The code below does what you want, but the modebar is not nice in the downloaded html.

library(plotly)

set.seed(666L)
asd <- data.frame(
  week = c(1, 2, 3, 4, 5, 6, 7, 8), 
  a    = rpois(8L, 30), 
  b    = rpois(8L, 25)
)

js <- c(
  'function(gd) {',
  '  var html = gd.innerHTML;',
  '  var head = "<head><meta charset=\\\"utf-8\\\"><script src=\\\"https://cdn.plot.ly/plotly-2.11.1.min.js\\\"></script>";',
  '  var body = "<body>" + html + "</body>";',
  '  var htmlContent = [head + body];',
  '  var bl = new Blob(htmlContent, {type: "text/html"});',
  '  var a = document.createElement("a");',
  '  a.href = URL.createObjectURL(bl);',
  '  a.download = "plotly.html";',
  '  document.body.appendChild(a);',
  '  a.click();',
  '}'
)

SVGicon_path <- "M 23.128906 25.402344 L 19.601562 25.402344 C 19.324219 25.402344 19.097656 25.175781 19.097656 24.890625 L 19.097656 15.6875 L 10.03125 15.6875 L 10.03125 24.890625 C 10.03125 25.175781 9.804688 25.402344 9.527344 25.402344 L 6 25.402344 C 5.722656 25.402344 5.496094 25.175781 5.496094 24.890625 L 5.496094 3.414062 C 5.496094 3.132812 5.722656 2.902344 6 2.902344 L 9.527344 2.902344 C 9.804688 2.902344 10.03125 3.132812 10.03125 3.414062 L 10.03125 11.597656 L 19.097656 11.597656 L 19.097656 3.414062 C 19.097656 3.132812 19.324219 2.902344 19.601562 2.902344 L 23.128906 2.902344 C 23.40625 2.902344 23.632812 3.132812 23.632812 3.414062 L 23.632812 24.890625 C 23.632812 25.175781 23.40625 25.402344 23.128906 25.402344 Z M 23.128906 25.402344 "
downloadHTML <- list(
  name = "Download HTML",
  icon = list(
    path   = SVGicon_path,
    width  = 30,
    height = 30
  ),
  click = htmlwidgets::JS(js)
)

plot_ly(
  asd, x = ~week, y = ~a, name = "a", type = "scatter", mode = "lines"
) %>%
  add_trace(y = ~b, name = "b", mode = "lines") %>%
  layout(
    margin = list(l = 100, r = 100, b = 100, t = 100), 
    xaxis = list(
      title     = "Week", 
      showgrid  = FALSE, 
      rangemode = "normal"
    ),
    yaxis = list(
      title     = "", 
      showgrid  = FALSE, 
      rangemode = "tozero"
    ),
    hovermode = "x unified"
  ) %>%
  config(
    modeBarButtonsToAdd = list(downloadHTML)
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM