r - 显示 R Markdown 中的 Seaborn 绘图 (flexdashboard)

标签 r r-markdown flexdashboard reticulate

我想知道是否有人对使用 {reticulate} 在 R Markdown 中显示 Seaborn Python 绘图有任何见解。

为了重现性,以下是本文中的示例:https://towardsdatascience.com/python-seaborn-plots-in-r-using-reticulate-fb59cebf61a7

设置:

library(reticulate)
#importing required Python libraries/modules
sns <- import('seaborn')
plt <- import('matplotlib.pyplot')
pd <- import('pandas')

图表:

#using R's inbuilt AirPassengers dataset
df <- datasets::AirPassengers
#converting Time-Series object into an R Dataframe 
#Thx: https://stackoverflow.com/questions/5331901/transforming-a-time-series-into-a-data-frame-and-back
df1 <- data.frame(tapply(df, list(year = floor(time(df)), month = month.abb[cycle(df)]), c))
df1 <- df1[month.abb]
#building a heatmap using seaborn 
#please note the function r_to_py() that converts R object into a python 
sns$heatmap(r_to_py(df1), fmt="g", cmap ='viridis')
#display the plot
plt$show()

这将按预期显示在 RStudio 绘图 Pane 中: enter image description here

但是在编织 .Rmd 文件(flexdashboard)时生成相同的绘图,它会弹出一个查看器,在我关闭它后,报告会以这种方式呈现: enter image description here

Flexdashboard 代码(代码块上有两个反引号,而不是 3 个反引号用于格式化):

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

``{r setup, include=FALSE}
library(reticulate)
#importing required Python libraries/modules
sns <- import('seaborn')
plt <- import('matplotlib.pyplot')
pd <- import('pandas')
``

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

``{r}
#using R's inbuilt AirPassengers dataset
df <- datasets::AirPassengers
#converting Time-Series object into an R Dataframe 
#Thx: https://stackoverflow.com/questions/5331901/transforming-a-time-series-into-a-data-frame-and-back
df1 <- data.frame(tapply(df, list(year = floor(time(df)), month = month.abb[cycle(df)]), c))
df1 <- df1[month.abb]
#building a heatmap using seaborn 
#please note the function r_to_py() that converts R object into a python 
sns$heatmap(r_to_py(df1), fmt="g", cmap ='viridis')
#display the plot
plt$show()
``

关于如何在 flexdashboard 中显示 seaborn 情节有什么想法吗?

最佳答案

避免显示绘图,将seaborn绘图保存到文件中,然后使用Markdown查看它。虽然有点难看,但确实有效。

```{r}
#using R's inbuilt AirPassengers dataset
df <- datasets::AirPassengers
#converting Time-Series object into an R Dataframe 
#Thx: https://stackoverflow.com/questions/5331901/transforming-a-time-series-into-a-data-frame-and-back
df1 <- data.frame(tapply(df, list(year = floor(time(df)), month = month.abb[cycle(df)]), c))
df1 <- df1[month.abb]
#building a heatmap using seaborn 
#please note the function r_to_py() that converts R object into a python 
# assign the result of the heatmap to a variable so it doesn't get shown
ss <- sns$heatmap(r_to_py(df1), fmt="g", cmap ='viridis')
#save the plot
plt$savefig('passenger_heatmap.png')
# Avoid the pop up by not showing the plot
#plt$show()
```

![heatmap](passenger_heatmap.png)

关于r - 显示 R Markdown 中的 Seaborn 绘图 (flexdashboard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70221248/

相关文章:

r - 如何在使用 Shiny Flexdashboard 生成的页面中添加页脚

r - ggplot2 中带有边缘直方图的散点图

r - 有没有办法将带有表情符号的文本写入 Excel 或 Google 表格?

r - 仅将 tidyr 单独应用于特定行

RMarkdown 文档 - 如何延迟内联代码段的 knitr 评估,直到处理完以后的 block ?

Shiny:Shiny 应用程序中的 flexdashboard 仪表

带分数的 r 标签图

r - 应用 CSS 使 ioslides 样式的代码更小

r - 如何在flexdashboard中组合行和列布局?

r - 用DT包输出时可以更改R默认表长度吗?