附录 E 混合编程

R 与不同的编程语言如何交互

E.1 R 与 JavaScripts

library(htmlwidgets)

E.2 R 与 Python

R 包 knitr 和 reticulate 支持 R Markdown 文档中嵌入 Python 代码块, reticulate 包还支持 Python 和 R 之间的数据对象通信交流。

library(reticulate)

如图 E.1 所示,在 R Markdown 中执行 Python 绘图代码,并且将图形插入文档。

import matplotlib.pyplot as plt
plt.switch_backend('agg')

plt.plot([0, 2, 1, 4])
plt.show()
Python 图形

图 E.1: Python 图形

E.3 R 与 C

knitr 支持在 R Markdown 中嵌入 C 语言代码

void useC(int *i){
  i[0] = 11;
}
## gcc -I"/opt/R/R-3.6.3/lib64/R/include" -DNDEBUG   -I/usr/local/include  -fpic  -g -O2  -c cc6c7b7338a4.c -o cc6c7b7338a4.o
## gcc -shared -L/opt/R/R-3.6.3/lib64/R/lib -L/usr/local/lib64 -o cc6c7b7338a4.so cc6c7b7338a4.o -L/opt/R/R-3.6.3/lib64/R/lib -lR
a <- rep(2,10)
out <- .C("useC", b = as.integer(a))
out
## $b
##  [1] 11  2  2  2  2  2  2  2  2  2
out$b
##  [1] 11  2  2  2  2  2  2  2  2  2

一步一步地命令行操作

R CMD SHLIB useC1.c
dyn.load("useC1.dll")
a <- rep(2,10)
out <- .C("useC", b = as.integer(a))
out$b

E.4 R 与 C++

Dirk Eddelbuettel Introduction to Rcpp: from simple examples to machine learning James Balamuta unofficial rcpp api documentation

library(Rcpp)

E.5 R 与 LaTeX

tikzDevice 包将 LaTeX 公式和绘图系统 TikZ 引入 R 语言生态,贡献在于提供更加漂亮的公式输出,对图形进行后期布局排版加工,达到设计师出品的质量水平。图 E.2 展示了复杂的 TeX 生态系统, R 语言只是取其精华,使用 TikZ 绘制。

\usetikzlibrary{mindmap}
\begin{tikzpicture}
  \path [
    mindmap,
    text = white,
    level 1 concept/.append style =
      {font=\Large\bfseries\sffamily, sibling angle=90, level distance=125},
    level 2 concept/.append style =
      {font=\normalsize\bfseries\sffamily},
    level 3 concept/.append style =
      {font=\small\bfseries\sffamily},
    tex/.style     = {concept, ball color=blue,
      font=\Huge\bfseries},
    engines/.style = {concept, ball color=green!50!black},
    formats/.style = {concept, ball color=purple!50!black},
    systems/.style = {concept, ball color=red!90!black},
    editors/.style = {concept, ball color=orange!90!black}
  ]
  node [tex] {\TeX} [clockwise from=0]
    child[concept color=green!50!black, nodes={engines}] {
      node {Engines} [clockwise from=90]
        child { node {\TeX} }
        child { node {pdf\TeX} }
        child { node {XeTeX} }
        child { node {Lua\TeX} }}
    child [concept color=purple, nodes={formats}] {
      node {Formats} [clockwise from=300]
        child { node {\LaTeX} }
        child { node {Con\TeX t} }}
    child [concept color=red, nodes={systems}] {
      node {Systems} [clockwise from=210]
        child { node {\TeX Live} [clockwise from=300]
          child { node {Mac \TeX} }}
        child { node {MiK\TeX} [clockwise from=60]
          child { node {Pro \TeX t} }}}
    child [concept color=orange, nodes={editors}] {
      node {Editors} [clockwise from=180]
        child { node {WinEdt} }
        child { node {\TeX works} }
        child { node {\TeX studio} }
        child { node {\TeX maker} }};
\end{tikzpicture}
TeX 系统

图 E.2: TeX 系统

E.6 运行环境

sessionInfo()
## R version 3.6.3 (2020-02-29)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: CentOS Linux 8 (Core)
## 
## Matrix products: default
## BLAS:   /opt/R/R-3.6.3/lib64/R/lib/libRblas.so
## LAPACK: /opt/R/R-3.6.3/lib64/R/lib/libRlapack.so
## 
## locale:
## [1] en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods  
## [7] base     
## 
## other attached packages:
## [1] Rcpp_1.0.4.12     reticulate_1.16   htmlwidgets_1.5.1
## [4] shiny_1.5.0       magrittr_1.5     
## 
## loaded via a namespace (and not attached):
##  [1] knitr_1.29      xtable_1.8-4    lattice_0.20-41 R6_2.4.1       
##  [5] rlang_0.4.6     fastmap_1.0.1   highr_0.8       stringr_1.4.0  
##  [9] tools_3.6.3     grid_3.6.3      xfun_0.15       htmltools_0.5.0
## [13] yaml_2.2.1      digest_0.6.25   bookdown_0.20   Matrix_1.2-18  
## [17] later_1.1.0.1   promises_1.1.1  evaluate_0.14   mime_0.9       
## [21] rmarkdown_2.3   stringi_1.4.6   compiler_3.6.3  jsonlite_1.6.1 
## [25] httpuv_1.5.4