-
-
Notifications
You must be signed in to change notification settings - Fork 878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plot created outside of knit function somehow sneaks into knit_child #2166
Comments
This sounds weird. @cderv Are you able to reproduce the problem? |
Hi, yes I am able to reproduce. See Details below Details
Closing the graphic device before knitting fix the issue. plot(1:10)
dev.off() # adding this fix the issue - no plot included as image
knitr::knit("test.Rmd") Details
it seems knitr will indeed use an existing open one right ? Lines 201 to 208 in 907184f
It could be link to this. It is also happening only when using ---
title: "Untitled"
output: html_document
---
```{r}
print("I am main.")
res <- knitr::knit_child("child.Rmd", quiet = TRUE)
res
``` will show us this in knitr ```r
res <- knitr::knit_child("child.Rmd", quiet = TRUE)
res
```
![plot of chunk unnamed-chunk-1](figure/unnamed-chunk-1-1.png)
```
## [1] "\n\n```r\nprint(\"I am the child.\")\n```\n\n```\n## [1] \"I am the child.\"\n```"
``` So it does not seem to be inside Can't you reproduce ? Is this a windows issue ? |
@cderv I didn't try to reproduce (just asking you for help instead). I don't remember why I wanted to avoid opening a new device when an existing device is open (i.e., @niklaswillrich Would it be much trouble for you to close the device after the plot (i.e., |
If I call dev.off() after plot() the figure is not inserted anymore. The main.md now looks like this:
I will force dev.off() before all the rendering of Rmarkdown in my original project as a workaround for now and wait for a possible bug fix. Still happy to test stuff provide further information. Thanks for the quick reply and help! |
@yihui Ok great no problem. I'll look closer to this - I want to understand. That is an unexpected behavior.
@niklaswillrich is this happening with The example shared only used knitr to render the Rmd to md and not rmarkdown completely. |
@cderv yes, this is happening with rmarkdown::render too. If you replace |
Just as a clarification: dev.off() solves the problem for rmarkdown::render too. |
I have finally figured out and fixed this weird problem. You can install the development version via remotes::install_github('yihui/knitr') Thanks! |
…usly otherwise it can cause a problem here: https://github.com/cran/monitoR/blob/e07ae7423dbfd08eeaf1a5916060103761391e35/R/Methods-plot.R#L139 because dev.set(1) (i.e. set the null device) will make dev.list() a vector of length > 1 (although I don't understand why), and `if (dev.list() == 2)` will throw an error "the condition has length > 1"
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary. |
I have a problem where knitting a child document according to the recommendation under
https://bookdown.org/yihui/rmarkdown-cookbook/child-document.html results in some weird behaviour.
A plot which was printed in the global environment is inserted in the output of knitr.
This has happened in a more complex setup where I was rendering Rmarkdown files with Child-Documents. I have tried to create a minimal reproducible example as follows:
I have a main.Rmd with the following syntax:
and I have a child.Rmd
I then run
I get the following main.md as output:
The plot which was created outside of the knit-function is now inside the document. I could reproduce this problem for different R versions and different machines.
Thanks!
P.S.: I am happy to supplement additional infos etc. if you need it.
Session info:
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Locale:
LC_COLLATE=German_Germany.utf8
LC_CTYPE=German_Germany.utf8
LC_MONETARY=German_Germany.utf8
LC_NUMERIC=C
LC_TIME=German_Germany.utf8
Package version:
evaluate_0.16 glue_1.6.2 graphics_4.2.1
grDevices_4.2.1 highr_0.9 knitr_1.40.1
magrittr_2.0.3 methods_4.2.1 stats_4.2.1
stringi_1.7.8 stringr_1.4.1 tools_4.2.1
utils_4.2.1 xfun_0.32 yaml_2.3.5
By filing an issue to this repo, I promise that
xfun::session_info('knitr')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/knitr')
.I understand that my issue may be closed if I don't fulfill my promises.
The text was updated successfully, but these errors were encountered: