-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmixed-programming.Rmd
200 lines (146 loc) · 5.98 KB
/
mixed-programming.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 混合编程 {#chap-mixed-programming}
```{r,include=FALSE}
library(magrittr)
library(shiny)
library(htmlwidgets)
library(reticulate)
library(Rcpp)
knitr::opts_chunk$set(python.reticulate = TRUE)
reticulate::use_virtualenv(virtualenv = Sys.getenv("RETICULATE_PYTHON_ENV"), required = TRUE)
```
R 语言 [@Ross_1996_R] 是一个统计计算和绘图的环境,以下各个节不介绍具体 R 包函数用法和参数设置,重点在历史发展趋势脉络,详细介绍去见[《现代统计图形》](https://bookdown.org/xiangyun/msg)的相应章节。R 语言的目标在于统计计算和绘图,设计优势在数据结构、图形语法、动态文档和交互图形
<!-- 介绍 R 与其它语言的异同,降低编程门槛 -->
## 函数源码 {#sec-function-source}
[flow](https://github.com/moodymudskipper/flow) 包可以将函数调用的过程以流程图的方式呈现,代码结构一目了然,快速理清源代码
```{r,eval=FALSE}
remotes::install_github('moodymudskipper/funflow')
funflow::view_flow('median.default')
```
```{r}
methods(predict)
```
stats 包里找不到这个函数
```{r}
ls("package:stats", all.names = TRUE, pattern = "predict.poly")
```
```{r,error=TRUE}
predict.poly
```
可见函数 `predict.poly()` 默认没有导出
```{r}
stats:::predict.poly
```
或者
```{r}
getAnywhere(predict.poly)
```
```{r}
getAnywhere("predict.poly")$where
```
函数参数个数
```{r}
names(formals(read.table))
```
## 命名约定 {#sec-naming-conventions}
R 语言当前的命名状态 <https://journal.r-project.org/archive/2012-2/RJournal_2012-2_Baaaath.pdf> 和 <https://essentials.togaware.com/StyleO.pdf>
R 与不同的编程语言如何交互
## R 与 JavaScripts {#sec-r-javascripts}
<!-- roughviz.js 为例介绍如何使用 htmlwidgets 制作将 JavaScripts 库打包成 R 包 -->
```{r}
library(htmlwidgets)
```
## R 与 Python {#sec-r-python}
R 包 knitr 和 reticulate 支持 R Markdown 文档中嵌入 Python 代码块, reticulate 包还支持 Python 和 R 之间的数据对象通信交流。
```{r}
library(reticulate)
```
如图 \@ref(fig:reticulate-matplotlib) 所示,在 R Markdown 中执行 Python 绘图代码,并且将图形插入文档。
```{python reticulate-matplotlib,fig.cap="Python 图形",collapse=TRUE, dev = ifelse(knitr::is_html_output(), 'svg', ifelse(knitr::is_latex_output(), 'pdf', 'png')), out.width='.8\\textwidth'}
import matplotlib.pyplot as plt
plt.switch_backend('agg')
plt.plot([0, 2, 1, 4])
plt.show()
```
## R 与 C {#sec-r-c}
knitr 支持在 R Markdown 中嵌入 C 语言代码
```{c}
void useC(int *i){
i[0] = 11;
}
```
```{r}
a <- rep(2,10)
out <- .C("useC", b = as.integer(a))
out
out$b
```
一步一步地命令行操作
```bash
R CMD SHLIB useC1.c
```
```r
dyn.load("useC1.dll")
a <- rep(2,10)
out <- .C("useC", b = as.integer(a))
out$b
```
## R 与 C++ {#sec-r-cpp}
[Dirk Eddelbuettel](https://dirk.eddelbuettel.com) 是 Rcpp 的核心开发者。
- Dirk Eddelbuettel celebRtion 2020, Copenhagen, Denmark [Introduction to Rcpp: from simple examples to machine learning](https://dirk.eddelbuettel.com/papers/celebRtion_feb2020_rcpp.pdf)
- Online Tutorial for useR! 2020 [Seamless R and C++ Introduction with Rcpp](https://dirk.eddelbuettel.com/papers/useR2020_rcpp_tutorial.pdf) 视频 https://vimeo.com/438283959
- James Balamuta [unofficial rcpp api documentation](https://thecoatlessprofessor.com/programming/unofficial-rcpp-api-documentation/) <https://github.com/coatless/rcpp-api>
- Rcpp for everyone <https://github.com/teuder/rcpp4everyone_en>
- 课程 [Foundations of Data Science](https://ds4ps.org/cpp-526-sum-2020/)
```{r}
library(Rcpp)
```
## R 与 LaTeX {#sec-r-latex}
<!-- TeX 的历史一小段介绍,tikzDevice 对公式的加强, 引入 TikZ 图形,常用 LaTeX 语法 <https://wch.github.io/latexsheet/latexsheet-a4.pdf> -->
**tikzDevice** 包将 LaTeX 公式和绘图系统 [TikZ](https://en.wikipedia.org/wiki/PGF/TikZ) 引入 R 语言生态,贡献在于提供更加漂亮的公式输出,对图形进行后期布局排版加工,达到设计师出品的质量水平。图 \@ref(fig:tex-system) 展示了复杂的 TeX 生态系统, R 语言只是取其精华,使用 TikZ 绘制。
```{r tex-system, engine="tikz", fig.cap="TeX 系统", cache=TRUE, engine.opts=list(extra.preamble=c("\\usepackage[default]{sourcesanspro}","\\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}
```
## 运行环境 {#sec-mixed-programming-session-info}
```{r}
sessionInfo()
```