rstudio / reticulate

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

Reticulate output intermediate seaborn plots #1047

Open rgaiacs opened 2 years ago

rgaiacs commented 2 years ago

Asking here as I didn't get a reply at community.rstudio.com.

Consider the following minimal working example

---
title: MWE
author: "Raniere Silva"
---

```{r include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

reticulate::use_condaenv('faxitron')
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

(ref:mwe) Some text.

data = {
  'c': ['R', 'T', 'R', 'T'],
  'r': ['1', '1', '2', '2'],
  'x': [1, 1, 1, 1],
  'y': [1, 2, 2, 1]
} 

df = pd.DataFrame(data)

g = sns.FacetGrid(
  df,
  col="c",
  row="r",
  sharex=False,
  sharey=False,
  despine=False,
  subplot_kws=dict(projection='polar'),
  palette="Blues"
)
_ = g.map_dataframe(
  sns.scatterplot,
  x="x",
  y="y"
)
_ = g.set_titles(
  row_template="{row_name}",
  col_template="{col_name}"
)
plt.show()

When the .Rmd file is converted to HTML, the intermediate seaborn plots are included:

![screenshot](https://user-images.githubusercontent.com/1506457/131427610-d2c794d4-63b6-4f51-a866-00d04348a7e4.jpeg)

How can I keep only the first one?

# Session Info

sessionInfo() R version 4.0.2 (2020-06-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.10

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=en_GB.UTF-8 LC_NUMERIC=C LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8 LC_PAPER=en_GB.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached): [1] Rcpp_1.0.7 bookdown_0.22 lattice_0.20-41 png_0.1-7 digest_0.6.27 rappdirs_0.3.3
[7] grid_4.0.2 jsonlite_1.7.2 evaluate_0.14 rlang_0.4.11 rstudioapi_0.13 Matrix_1.2-18
[13] reticulate_1.20 rmarkdown_2.10 tools_4.0.2 xfun_0.25 yaml_2.2.1 compiler_4.0.2
[19] htmltools_0.5.1.1 knitr_1.33

rafael-savvides commented 1 year ago

The issue is assigning to _. If you instead assign to a variable, you get one plot.

Replace:

_ = g.map_dataframe(
  sns.scatterplot,
  x="x",
  y="y"
)
_ = g.set_titles(
  row_template="{row_name}",
  col_template="{col_name}"
)

with:

tmp = g.map_dataframe(
  sns.scatterplot,
  x="x",
  y="y"
)
tmp = g.set_titles(
  row_template="{row_name}",
  col_template="{col_name}"
)

Session info

R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3    lattice_0.20-45 png_0.1-7       digest_0.6.29   R6_2.5.1        grid_4.2.2      jsonlite_1.8.0  magrittr_2.0.3  evaluate_0.15   stringi_1.7.6   rlang_1.0.2     cli_3.3.0       jquerylib_0.1.4 bslib_0.3.1    
[15] Matrix_1.5-1    reticulate_1.26 rmarkdown_2.17  tools_4.2.2     stringr_1.4.0   yaml_2.3.5      xfun_0.30       fastmap_1.1.0   compiler_4.2.2  htmltools_0.5.2 knitr_1.39      sass_0.4.1