Search code examples
rggplot2plotlyggplotlyr-plotly

R chart convert to html format without other files


My sample dataset:

library(plotly)
library(htmlwidgets)

d <-
  data.frame(
    "name" = c("bily", "mary", "ken"),
    "fruit" = c("cherry", "apple", "apple"),
    "count" = c(1, 10, 30)
  )

gg <- ggplot(data = d,
             aes(x = fruit,
                 y = count,
                 group = name)) +
  geom_line(aes(color = name)) +
  geom_point(aes(color = name))

plotly <- ggplotly(gg)

save graph:

d <- # please put your directory where you want to save the graph
  "xxxx/graph.html"

I have searched these three functions that can output the HTML format but they will also make other files and I cannot solve it.

  #htmlwidgets::saveWidget(as_widget(plotly), d)
  #htmltools::browsable(plotly)
  htmltools::save_html(plotly, d)

I am trying to convert my graph, which uses from ggplot function to ggplotly function, to HTML format but I face a problem. When I save the graph, the function will also make other files such as plotlyjs, jquery, and crosstalk. I hope only the HTML file can be made because it is too hard to put the files on the website if each graph has different files.

These are the files when I run the code. My expected result is that only the HTML file is made.

enter image description here

The other files are saved in the directory of the lib but I hope they are not provided and I can run the HTML file. Is it possible to do? or Are there any other methods?

enter image description here


Solution

  • Just add selfcontained = TRUE to your last line:

    htmlwidgets::saveWidget(as_widget(plotly), selfcontained = TRUE, file = "xxxx/graph.html")