R bookdown: 'r' chunk blocks graphic output from 'python' chunk

Viewed 105

Two {python} chunks produce plots using matplotlib. If I place an {r} chunk between them, then knitr replaces the first plot by the second. Anyone know how to fix this behavior?

Minimal example Rmd file:

--- 
title: "Error demo"
date: "`r Sys.Date()`"
---
    
```{r setup, include=FALSE}
library(reticulate)
use_condaenv('/Users/XXXX/opt/anaconda3/envs/r-env1', required = TRUE)
```
    
```{python, echo=FALSE}
import matplotlib.pyplot as plt
plt.plot([4,1,2,0],'r-')
```
    
```{r, echo = FALSE}
# an r chunk between the python chunks suppresses the first plot
```
    
```{python, echo=FALSE}
plt.plot([0,2,1,4],'g-')
```

The build output contains two versions of the second plot. Here is the relevant part of the HTML output:

<div id="header">
<h1 class="title">Error demo</h1>
<p class="date" style="margin-top: 1.5em;"><em>2021-05-07</em></p>
</div>
<p><img src="MIB-R_files/figure-html/unnamed-chunk-1-1.png" width="672" /></p>
<p><img src="MIB-R_files/figure-html/unnamed-chunk-1-1.png" width="672" /></p>

Version info:

  • MacOS: Darwin 20.3.0
  • Python: 3.9.2 | packaged by conda-forge
  • RStudio: 1.4.1106
  • rmarkdown: 2.8
  • knitr: 1.33
  • reticulate: 1.20
1 Answers

Resolved this by rebuilding the entire R environment. Now my bookdown project builds fine. Maybe there was some conflict between packages. In case you run into a similar problem, here is my current version list of R packages:

askpass 1.1 base64enc 0.1-3 BH 1.72.0-3 bookdown 0.21.6 brew 1.0-6 brio 1.1.2 bslib 0.2.4 cachem 1.0.4 callr 3.7.0 cli 2.5.0 clipr 0.7.1 commonmark 1.7 crayon 1.3.4 credentials 1.3.0 curl 4.3 desc 1.3.0 devtools 2.4.1 diffobj 0.3.4 digest 0.6.27 ellipsis 0.3.1 evaluate 0.14 fansi 0.4.2 fastmap 1.1.0 fs 1.5.0 gert 1.3.0 gh 1.3.0 gitcreds 0.1.1 glue 1.4.2 highr 0.8 htmltools 0.5.1.1 httpuv 1.5.4 httr 1.4.2 ini 0.3.1 jquerylib 0.1.3 jsonlite 1.7.2 knitr 1.30.4 later 1.1.0.1 lifecycle 0.2.0 magrittr 2.0.1 markdown 1.1 memoise 2.0.0 mime 0.9 openssl 1.4.3 packrat 0.6.0 pillar 1.6.0 pkgbuild 1.2.0 pkgconfig 2.0.3 pkgload 1.2.1 png 0.1-7 praise 1.0.0 prettyunits 1.1.1 processx 3.5.2 promises 1.1.1 ps 1.6.0 purrr 0.3.4 R6 2.5.0 rappdirs 0.3.1 rcmdcheck 1.3.3 Rcpp 1.0.5 rematch2 2.1.2 remotes 2.3.0 renv 0.13.2 reticulate 1.18-9000 rlang 0.4.9 rmarkdown 2.6.0003 roxygen2 7.1.1 rprojroot 2.0.2 rsconnect 0.8.16 rstudioapi 0.13 rversions 2.0.2 sass 0.3.1 sessioninfo 1.1.1 shiny 1.6.0 sourcetools 0.1.7 stringi 1.5.3 stringr 1.4.0 sys 3.4 testthat 3.0.2 tibble 3.1.1 tinytex 0.28 usethis 2.0.1 utf8 1.2.1 vctrs 0.3.8 waldo 0.2.5 whisker 0.4 withr 2.3.0 xfun 0.19 xml2 1.3.2 xopen 1.0.0 xtable 1.8-4 yaml 2.2.1 zip 2.1.1

Related