0

In a Rmd file, there are several places which generate plots, for example

```{r outliers}
influencePlot (model1)
```

I knit the file into a html file and several image files under directory ./figure/, by running in a R session

> library(knitr)
> knit("my.Rmd")

and then running in a bash session

$ pandoc my.md -f markdown -t html -s -o my.html

but the html file doesn't embed the plots from the image files, for example:

<img src="figure/outliers-1.png" alt="plot of chunk outliers" />

I was wondering how I could make the html file embed the plots from the image files and become self contained?

In RStudio, when I click "knit to html" button, it embeds the plot images in html file, for example

<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhE...

What is the equivalent commands to RStudio's "knit to html" button?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • 1
    Could you provide a minimal reproducible example of your Rmd? I just checked for a simple Rmd. Both `knit`ting and `pandoc`ing worked fine and plots showed up in the html. – stefan Apr 20 '20 at 06:08
  • 1
    Why not use `rmarkdown::render()`? – J_F Apr 20 '20 at 07:54
  • @stefan: see my update. with what is meant by embedding a plot in html file. The essential of the problem is that with the way that I create html from Rmd doesn't embed plots in html files, regardless of what plots they are what Rmd files they are. RStudio's "knit to html" button embeds plots in html files. What is the equivalent commands to RStudio's "knit to html" button? – Tim Apr 20 '20 at 10:18
  • @J_F What is the difference between rmarkdown::render() and knitr:knit()? Using the former doesn't embed plots in html files by default. – Tim Apr 20 '20 at 10:19
  • 1
    @Tim. Hm. Okay. Now as you pointed it out I see what you meant. In case you are looking for a `knit`+ `pandoc` solution to get the same output as by pressing the knit button I'm afraid I don't know about that. However, if you are only looking for a command-line version of the knit button then I think @J_F is right. Simply use `rmarkdown::render`. As with knit button plots are embedded in the html as ` – stefan Apr 20 '20 at 10:54
  • This post might be helpful to you: https://stackoverflow.com/q/40563479/559676 Or see a shorter version here: https://bookdown.org/yihui/rmarkdown-cookbook/r-markdown-components.html – Yihui Xie Apr 20 '20 at 15:34

1 Answers1

2

What is the equivalent commands to RStudio's "knit to html" button?

Submit your output in a YAML header and then use

rmarkdown::render("Your-RMD-file.Rmd")

Or see the documentation for the render function and their arguments.

J_F
  • 9,956
  • 2
  • 31
  • 55