如何使用knitr和plotly在html文档中嵌入曲面图?
我认为下面的R Markdown(Rmd)代码应该生成一个嵌入了曲面图的html文档如何使用knitr和plotly在html文档中嵌入曲面图?,r,plot,plotly,R,Plot,Plotly,我认为下面的R Markdown(Rmd)代码应该生成一个嵌入了曲面图的html文档 ```{r, plotly=TRUE} library(plotly) py <- plotly() x_vec = c(seq(-5, 4.9, 0.1)) x_matrix = matrix(c(x_vec), nrow = 100, ncol = 1) y_matrix = matrix(c(x_vec), nrow = 1, ncol = 100) data <- list( x =
```{r, plotly=TRUE}
library(plotly)
py <- plotly()
x_vec = c(seq(-5, 4.9, 0.1))
x_matrix = matrix(c(x_vec), nrow = 100, ncol = 1)
y_matrix = matrix(c(x_vec), nrow = 1, ncol = 100)
data <- list(
x = x_vec,
y = x_vec,
z = matrix(c(cos(x_matrix %*% y_matrix) + sin(x_matrix %*% y_matrix)), nrow = 100, ncol = 100),
type = "surface")
layout <- list(
title = "Waaaves in r",
scene = list(bgcolor = "rgb(244, 244, 248)"))
response <- py$plotly(data, session="knitr",
kwargs = list(
layout = layout,
world_readable=FALSE,
filename = "waves example",
fileopt = "overwrite"))
```
`{r,plotly=TRUE}
图书馆(绘本)
py运行py$plotly()
,而不是运行py$irplot()
(不带session=“knitr”
参数)。后者使用的是py$ggplotly()
方法,而不是py$plotly()
方法。是否尝试不使用plotly选项?或者更好,您试图避免什么?此解决方案有效吗(发布文档)?@Llopis如果我删除参数session=“knitr”,则不会出现错误消息。但是,该图形不包含在html文档中。@Llopis如果我使用py$ggplotly方法而不是py$plotly方法,则该方法有效,并且该绘图包含在html文档中。(我使用了不同的数据集来测试。)@Mateo您发布的解决方案运行良好。然而,当我想做的只是将曲面图作为一些探索性数据分析的一部分时,这有点麻烦。