简体   繁体   English

如何使用knitr :: spin将R脚本转换为带有交互式图的html文件

[英]how to use knitr::spin to convert R script to html file with interactive plots

I want to generate some Rmarkdown file from r script using knitr::spin with interactive plots. 我想使用knitr::spin和交互式图从r脚本生成一些Rmarkdown文件。 but the plots it generates are static and located in the figure subfolder. 但它生成的图是静态的,位于图子文件夹中。 How could I generate keep the interactive plots? 我怎样才能生成交互式图?

here is an example: 这是一个例子:

#' ---
#' title: "test me"
#' output:
#'   html_notebook:
#'     code_folding: hide
#'     toc: true
#'     toc_float: true
#'     collapsed: false
#' ---
#+ warning=FALSE, echo=FALSE, message=FALSE
library(ggplot2)
library(plotly)
library(DT)

#' ## interactive cars
data(cars)
ggplotly(ggplot(data=cars,aes(x=speed,y=dist))+geom_point())

#' ## interactive table
datatable(cars)

I think this might be because you're making two calls for a widget in the same chunk. 我想这可能是因为你在同一块中对两个小部件进行两次调用。 When I run your code on RStudio as an Rmd file, the RNotebook file seems to stack the plots (which may be what you mean). 当我在RStudio上运行你的代码作为Rmd文件时,RNotebook文件似乎堆积了这些图(这可能是你的意思)。

When I separate the function calls between two chunks, though, all appears fine. 但是,当我将两个块之间的函数调用分开时,一切都显示正常。 I generate the following output (which you can see here on RPubs): http://rpubs.com/p0bs/285465 . 我生成以下输出(您可以在RPubs上看到): http ://rpubs.com/p0bs/285465。 (In order to get the code, just click the dropdown in the RPubs document and click SAVE RMD .) (为了获取代码,只需单击RPubs文档中的下拉列表,然后单击SAVE RMD 。)

I hope this helps you. 我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM