【问题标题】:Can't generate plotly interactive plots in Rmarkdown report无法在 Rmarkdown 报告中生成交互式绘图
【发布时间】:2017-09-22 08:36:39
【问题描述】:

我有一个带有 plotly 绘图的 Rmarkdown 文档,我想生成一个 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 生成的图,但它不是交互式的。 有谁知道如何让它与命令行交互?

谢谢

这是代码,顺便说一句:

---
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()
```

【问题讨论】:

  • 检查this link,你必须使用htmltools::tags$....用于嵌入情节图
  • 不,实际上你没有。当我从 Rstudio 内部编织时,我确实得到了一个嵌入的情节图,但当我从命令行进行时却没有。那就是问题所在。我可能解释得有点不清楚。

标签: r knitr r-markdown plotly ggplotly


【解决方案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 参数来实现。

【讨论】:

  • 抱歉,rmarkdown::render([...]) 是什么意思?请提供更多详细信息。谢谢!
  • 你想要什么细节?
  • 哦,对不起,你只是说函数rmarkdown::render,我以为你在用方括号做一些花哨的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多