Python 代码块生成的图片不支持 caption? 自然也不支持图片的引用
在 knitr::opts_chunk
中设置 python.reticulate = TRUE
意味着所有的 Python 代码块共享一个 Python Session(好特性),而 python.reticulate = FALSE
意味着使用 knitr 提供的 Python 引擎,所有的 Python 代码块独立运行。
下面提供一个可复现问题的例子
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
python.reticulate = TRUE
)
```
## R Markdown
```{python math}
import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')
```
matplotlib 绘图
```{python math-demo,eval=FALSE}
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)
plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
plt.show()
```
```{python demo-matplotlib, ref.label = 'math-demo', fig.cap = "数学公式", eval = TRUE, echo = FALSE}
```
环境信息
xfun::session_info(c('rmarkdown','reticulate'))
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Locale:
LC_COLLATE=Chinese (Simplified)_China.936
LC_CTYPE=Chinese (Simplified)_China.936
LC_MONETARY=Chinese (Simplified)_China.936
LC_NUMERIC=C
LC_TIME=Chinese (Simplified)_China.936
Package version:
base64enc_0.1.3 digest_0.6.18 evaluate_0.13 glue_1.3.1
graphics_3.6.0 grDevices_3.6.0 grid_3.6.0 highr_0.8
htmltools_0.3.6 jsonlite_1.6 knitr_1.22 lattice_0.20.38
magrittr_1.5 markdown_0.9 Matrix_1.2.17 methods_3.6.0
mime_0.6 Rcpp_1.0.1 reticulate_1.12 rmarkdown_1.12
stats_3.6.0 stringi_1.4.3 stringr_1.4.0 tinytex_0.13
tools_3.6.0 utils_3.6.0 xfun_0.7 yaml_2.2.0
Pandoc version: 2.7.2
我感觉这个问题应该和环境信息关系不大,也和 Python 环境信息无关
xfun::session_info(c('knitr','rmarkdown','reticulate'))
R Under development (unstable) (2019-05-10 r76484)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Fedora 30 (Thirty), RStudio 99.9.9
Locale:
LC_CTYPE=zh_CN.UTF-8 LC_NUMERIC=C LC_TIME=zh_CN.UTF-8 LC_COLLATE=zh_CN.UTF-8
LC_MONETARY=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8 LC_PAPER=zh_CN.UTF-8 LC_NAME=C
LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C
Package version:
base64enc_0.1.3 digest_0.6.18 evaluate_0.13 glue_1.3.1
graphics_3.7.0 grDevices_3.7.0 grid_3.7.0 highr_0.8
htmltools_0.3.6 jsonlite_1.6 knitr_1.22.15 lattice_0.20.38
magrittr_1.5 markdown_0.9 Matrix_1.2.17 methods_3.7.0
mime_0.6 Rcpp_1.0.1 reticulate_1.12.0.9000 rmarkdown_1.12
stats_3.7.0 stringi_1.4.3 stringr_1.4.0 tinytex_0.13
tools_3.7.0 utils_3.7.0 xfun_0.7 yaml_2.2.0
Pandoc version: 2.3.1
参考 https://github.com/yihui/knitr/blob/332e28c647d85ae3937b1bcd7ac61c0aea54d897/NEWS.md#major-changes-4