本文介绍了R图表转换为HTML格式,没有其他文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的样本数据集:

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)

保存图表:

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

我已经搜索了可以输出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)

我正在尝试将从ggplot函数转换为ggplotly函数的图形转换为HTML格式,但是我遇到了问题.当我保存图形时,该函数还将创建其他文件,例如plotlyjs,jquery和crosstalk.我希望只能制作HTML文件,因为如果每个图形都有不同的文件,将文件放在网站上太难了.

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.

这些是我运行代码时的文件.我的预期结果是仅制作了HTML文件.

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

其他文件保存在lib目录中,但希望不提供这些文件,并且我可以运行HTML文件.有可能吗?或还有其他方法吗?

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?

推荐答案

只需将selfcontained = TRUE添加到最后一行:

Just add selfcontained = TRUE to your last line:

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

这篇关于R图表转换为HTML格式,没有其他文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:48