R chart convert to html format without other files

落花浮王杯 提交于 2019-12-13 16:09:29

问题


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.

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?


回答1:


Just add selfcontained = TRUE to your last line:

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


来源:https://stackoverflow.com/questions/48497704/r-chart-convert-to-html-format-without-other-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!