rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.68k stars 327 forks source link

Rmd python chunk output options do not work as expected #1144

Open Fan-iX opened 2 years ago

Fan-iX commented 2 years ago

For R chunks, chunk option results = "markup", fig.show="hold" will hide text output and place all figure after the chunk. However, the same option doesn't work for python chunks.

My knitr render result look like this:

results = "hide", fig.show="hold" output for R

The original chunk

```{r results = "markup", fig.show="hold"}
head(iris)
ggplot(iris) +
  geom_histogram(aes(Sepal.Length, fill=Species),
                 breaks=c(4,seq(4.5,7.5,0.2),8))
tail(iris)

output

```{r}
head(iris)
ggplot(iris) +
  geom_histogram(aes(Sepal.Length, fill=Species),
                 breaks=c(4,seq(4.5,7.5,0.2),8))
tail(iris)

R_iris

results = "hide", fig.show="hold" output for Python

the original chunk

```{python results = "hide", fig.show="hold"}
fig, ax = plt.subplots(constrained_layout=True)
ax.hist(
  r.iris.pivot(columns="Species", values="Sepal.Length"),
  histtype='bar', stacked=True, label=r.iris["Species"].unique(),
  bins=[4, *np.arange(4.5, 7.5, 0.2), 8.0]
)
ax.set_xlabel("Sepal.Length")
ax.set_ylabel("count")
plt.legend(bbox_to_anchor=(1.04,0.5),loc="center left")
plt.show()
plt.savefig(fname="name",format="svg")
plt.close()

output

```{python}
fig, ax = plt.subplots(constrained_layout=True)
ax.hist(
  r.iris.pivot(columns="Species", values="Sepal.Length"),
  histtype='bar', stacked=True, label=r.iris["Species"].unique(),
  bins=[4, *np.arange(4.5, 7.5, 0.2), 8.0]
)
ax.set_xlabel("Sepal.Length")
ax.set_ylabel("count")
plt.legend(bbox_to_anchor=(1.04,0.5),loc="center left")
plt.show()

Python_iris

plt.close()

Where the chunk is broken by hidden output (the result="hidden" option didn't work as it does in R chunks), and the figure is rendered right after plt.show()( the fig.show="hold" option didn't work).

Only if I set results = "hold" will the figure be placed after the whole chunk.

results = "hold", fig.show="hold" output for Python

the original chunk

```{python results = "hold", fig.show="hold"}
fig, ax = plt.subplots(constrained_layout=True)
ax.hist(
  r.iris.pivot(columns="Species", values="Sepal.Length"),
  histtype='bar', stacked=True, label=r.iris["Species"].unique(),
  bins=[4, *np.arange(4.5, 7.5, 0.2), 8.0]
)
ax.set_xlabel("Sepal.Length")
ax.set_ylabel("count")
plt.legend(bbox_to_anchor=(1.04,0.5),loc="center left")
plt.show()
plt.close()

output

```{python}
fig, ax = plt.subplots(constrained_layout=True)
ax.hist(
  r.iris.pivot(columns="Species", values="Sepal.Length"),
  histtype='bar', stacked=True, label=r.iris["Species"].unique(),
  bins=[4, *np.arange(4.5, 7.5, 0.2), 8.0]
)
ax.set_xlabel("Sepal.Length")
ax.set_ylabel("count")
plt.legend(bbox_to_anchor=(1.04,0.5),loc="center left")
plt.show()
plt.close()
## (array([[ 4.,  5.,  7., 20.,  4.,  7.,  2.,  1.,  0.,  0.,  0.,  0.,  0.,
##          0.,  0.,  0.],
##        [ 4.,  5.,  7., 24.,  5., 13., 12.,  6.,  8.,  5.,  3.,  5.,  2.,
##          1.,  0.,  0.],
##        [ 4.,  5.,  7., 25.,  5., 13., 14., 10., 12., 13., 12., 10.,  7.,
##          2.,  4.,  7.]]), array([4. , 4.5, 4.7, 4.9, 5.1, 5.3, 5.5, 5.7, 5.9, 6.1, 6.3, 6.5, 6.7,
##        6.9, 7.1, 7.3, 8. ]), <a list of 3 BarContainer objects>)

Python_iris

But in this case, I am not able to hide unwanted text output.

session info:

## R version 4.1.0 (2021-05-18)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
## 
## locale:
##  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
##  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
##  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
## [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] patchwork_1.1.1 forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7    
##  [5] purrr_0.3.4     readr_1.4.0     tidyr_1.1.3     tibble_3.1.2   
##  [9] ggplot2_3.3.5   tidyverse_1.3.1 reticulate_1.23
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.7       lubridate_1.7.10 lattice_0.20-44  png_0.1-7       
##  [5] assertthat_0.2.1 digest_0.6.27    utf8_1.2.1       R6_2.5.0        
##  [9] cellranger_1.1.0 backports_1.2.1  reprex_2.0.0     evaluate_0.14   
## [13] httr_1.4.2       highr_0.9        pillar_1.6.1     rlang_0.4.11    
## [17] readxl_1.3.1     rstudioapi_0.13  jquerylib_0.1.4  Matrix_1.3-4    
## [21] rmarkdown_2.11   labeling_0.4.2   munsell_0.5.0    broom_0.7.8     
## [25] compiler_4.1.0   modelr_0.1.8     xfun_0.29        pkgconfig_2.0.3 
## [29] htmltools_0.5.2  tidyselect_1.1.1 fansi_0.5.0      crayon_1.4.1    
## [33] dbplyr_2.1.1     withr_2.4.2      grid_4.1.0       jsonlite_1.7.2  
## [37] gtable_0.3.0     lifecycle_1.0.0  DBI_1.1.1        magrittr_2.0.1  
## [41] scales_1.1.1     cli_3.0.0        stringi_1.6.2    farver_2.1.0    
## [45] fs_1.5.0         xml2_1.3.2       ellipsis_0.3.2   generics_0.1.0  
## [49] vctrs_0.3.8      tools_4.1.0      glue_1.4.2       hms_1.1.0       
## [53] fastmap_1.1.0    yaml_2.2.1       colorspace_2.0-2 rvest_1.0.0     
## [57] knitr_1.37       haven_2.4.1
kevinushey commented 2 years ago

Because reticulate provides a custom knitr engine, we need to re-implement handling of these chunk options ourselves. Unfortunately, these particular options have not yet been implemented.