无法在Rmarkdown报表中生成绘图交互式绘图 [英] Can't generate plotly interactive plots in Rmarkdown report
问题描述
我有一个Rmarkdown文档,其中包含用plotly绘制的图,并且想生成一个html文件.当我在Rstudio中单击编织为HTML 时,它可以工作,但是当我在命令行中运行以下命令时,它就不工作了:
I have an Rmarkdown document with a plot made with plotly and would like to generate an html file. It works when I click on Knit to HTML in Rstudio, but not when I run the following on the command line:
Rscript -e "require(knitr)" -e "require(markdown)" -e "knit('Untitled.Rmd', out='report.md')" -e "markdownToHTML('report.md', 'report.html')"
在这之后,我有了一个report.html文件,其中包含用plotly生成的图,但它不是交互式的. 有谁知道如何使其与命令行交互?
After this, I have a report.html file which contains the plot generated with plotly, but it is not interactive. Does anyone know how to make it interactive with the command line?
谢谢
这是代码,顺便说一句:
This is the code, btw:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure_ggplot, echo=FALSE}
library(ggplot2)
library(plotly)
ggplot(pressure,aes(temperature,pressure))+geom_point(size=10,color='red')
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r pressure_plotly, echo=FALSE}
g<-ggplot(pressure,aes(temperature,pressure))+geom_point(size=10,color='red')
ggplotly(g)
```
```{r}
sessionInfo()
```
推荐答案
尝试:
Rscript -e "library(knitr); library(rmarkdown); rmarkdown::render('untitled.Rmd', output_file='report.html')"
理由:编织似乎默认为markdown输出,并且markdown不能包含HTML.因此,在转换时,您最终会丢失文件(如果从代码块的开口中删除echo=FALSE
,则会看到这些错误.
Reasoning: knit seems to default to markdown output, and markdown cannot contain HTML. Thus, when converting, you end up with missing files (you can see these errors if you remove echo=FALSE
from the code block openings.
rmarkdown::render([...])
可以清晰地呈现为HTML,从而避免了上述问题.如果希望指定输出格式,则可以使用output_format
参数.
rmarkdown::render([...])
renders cleanly to HTML, avoiding the issues mentioned above. If you wish to specify the output format, you can do so using the output_format
argument.
这篇关于无法在Rmarkdown报表中生成绘图交互式绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!