I'm using Rmarkdown in RStudio and including plots that are opened in new graphics windows. If the window is opened directly in a code chunk, then the plot is included in the processed document. But if the window is opened by a separate script that is sourced, then the plot appears during Knitr processing but is not included in the document. Here is a complete minimal example of an .Rmd script to demonstrate:
---
title: "Rmarkdown graph inclusion"
---
# Make a simple plot:
```{r}
plot(0,0,main="Attempt 1")
```
Result of above: Plot is displayed during processing and is included in generated document.
# Make the plot in a separate graphics window:
```{r}
windows() # open new graphics window; x11() on Linux, MacOS
plot(0,0,main="Attempt 2")
```
Result of above: Plot is displayed during processing and is included in generated document.
# Make the plot in a separate graphics window called from another script:
```{r}
writeLines( "windows() ; plot(0,0,main='From File')" ,
con="openWindowScript.R" )
source("openWindowScript.R")
```
Result of above: Plot **is** displayed during Knitr processing but is **NOT** included in the generated document. *Why not?*
I did search stackoverflow and elsewhere for an answer but didn't find one. Thanks in advance for answers or pointers!