在 R 中导入 plotly HTML 绘图

问题描述 投票:0回答:1

有没有办法导入使用

htmlwidgets::saveWidget
导出的 plotly plot?我用
xml2::read_html
尝试过,但是读取单个 html 文件缺少信息来再次获取完整列表,其中包含 plotly plot。

library(plotly)
library(htmlwidgets)
library(xml2)

# load the iris dataset
data(iris)

# create a scatter plot using plot_ly function
fig <- plot_ly(
  data = iris, 
  x = ~Petal.Length, 
  y = ~Petal.Width, 
  color = ~Species, 
  type = "scatter", 
  mode = "markers"
)

# Save the plot as an HTML file using `saveWidget()` function
saveWidget(fig, "my_plotly_plot.html", selfcontained = F)

# Read the HTML file and recreate the plot using `onRender()` function
fig.import <- read_html("my_plotly_plot.html")
r plotly
1个回答
0
投票

您可以使用

include_url
中的
knitr
将情节显示回 rmarkdown,例如:

---
title: "Untitled"
output: html_document
---

```{r}
library(plotly)
library(htmlwidgets)
library(knitr)

# load the iris dataset
data(iris)

# create a scatter plot using plot_ly function
fig <- plot_ly(
  data = iris, 
  x = ~Petal.Length, 
  y = ~Petal.Width, 
  color = ~Species, 
  type = "scatter", 
  mode = "markers"
)

# Save the plot as an HTML file using `saveWidget()` function
saveWidget(fig, "my_plotly_plot.html", selfcontained = F)

include_url('my_plotly_plot.html')
```

输出:


另一个选项可能是使用

browseURL
在浏览器中打开您的情节,如下所示:

browseURL("my_plotly_plot.html")
© www.soinside.com 2019 - 2024. All rights reserved.