无法在Rmarkdown报告中生成绘图交互式图

时间:2017-09-22 08:36:39

标签: r knitr r-markdown plotly ggplotly

我有一个Rmarkdown文档,其中有一个用plotly制作的图,并希望生成一个html文件。当我在Rstudio中单击 Knit to HTML 时,它可以工作,但是当我在命令行上运行以下命令时,它不起作用:

Rscript -e "require(knitr)" -e "require(markdown)" -e "knit('Untitled.Rmd', out='report.md')" -e "markdownToHTML('report.md', 'report.html')"

在此之后,我有一个report.html文件,其中包含使用plotly生成的图,但它不是交互式的。 有谁知道如何使它与命令行交互?

由于

这是代码,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()
```

1 个答案:

答案 0 :(得分:1)

尝试:

Rscript -e "library(knitr); library(rmarkdown); rmarkdown::render('untitled.Rmd', output_file='report.html')"

推理:knit似乎默认为markdown输出,而markdown不能包含HTML。因此,在转换时,您最终会丢失文件(如果从代码块开头删除echo=FALSE,则可以看到这些错误。

rmarkdown::render([...])干净利落地呈现HTML,避免了上述问题。如果要指定输出格式,可以使用output_format参数。