R图表转换为html格式,无需其他文件

R图表转换为html格式,无需其他文件,r,ggplot2,plotly,ggplotly,r-plotly,R,Ggplot2,Plotly,Ggplotly,R Plotly,我的示例数据集: 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,

我的示例数据集:

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)
我正在尝试将使用from ggplot函数到ggplotly函数的图形转换为HTML格式,但我面临一个问题。保存图形时,该函数还将生成其他文件,如plotlyjs、jquery和crosstalk。我希望只有HTML文件可以制作,因为如果每个图形都有不同的文件,那么将这些文件放到网站上太难了

这些是我运行代码时的文件。我的预期结果是只生成HTML文件

其他文件保存在lib的目录中,但我希望没有提供它们,我可以运行HTML文件。有可能吗?或者还有其他方法吗


只需在最后一行添加
selfcontained=TRUE

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

我试过了,但是还有其他文件,比如jquery、typedarray和htmlwidgets。是否可以使HTML文件只包含其他文件?我只是重新运行代码,看到两个文件:一个文件夹名为graph_files,另一个文件名为graph.HTML。你可以自己使用graph.html它不能,因为graph.html依赖于graph_文件,其中包括其他文件,如jquery、typedarray和htmlwidgets。不,只是将graph.html移动到另一个位置并打开它。是的,我知道你的意思,但如果graph_文件不在那里。html将不显示任何内容。
  #htmlwidgets::saveWidget(as_widget(plotly), d)
  #htmltools::browsable(plotly)
  htmltools::save_html(plotly, d)
htmlwidgets::saveWidget(as_widget(plotly), selfcontained = TRUE, file = "xxxx/graph.html")