我有一个带有绘图的绘图的rmarkdown文档,并希望生成一个HTML文件。当我在rstudio中单击编织到html 时,它可以工作,但是当我在命令行上运行以下内容时,它不行:
Rscript -e "require(knitr)" -e "require(markdown)" -e "knit('Untitled.Rmd', out='report.md')" -e "markdownToHTML('report.md', 'report.html')"
之后,我有一个report.html文件,其中包含与plitly生成的绘图,但不是交互式。有人知道如何使其与命令行互动吗?
谢谢
这是代码,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中没有保留HTMLWIDGETS。因此,转换时,您最终会丢失文件(如果您从代码块开口删除echo=FALSE
,则可以看到这些错误。
rmarkdown::render([...])
呈现为HTML,避免上述问题。如果您想指定输出格式,则可以使用output_format
参数。