-
Notifications
You must be signed in to change notification settings - Fork 586
/
023-engine-python.Rmd
73 lines (50 loc) · 1.38 KB
/
023-engine-python.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
R works, of course.
```{r test-r, engine='R'}
library(knitr)
set.seed(123)
rnorm(5)
```
Does **knitr** work with Python? Use the chunk option `engine='python'`:
```{r test-python, engine='python'}
x = 'hello, python world!'
print(x)
print(x.split(' '))
```
Or use the syntax ```` ```{python} ````:
```{python}
x = 'hello, python world!'
print(x.split(' '))
```
If all the chunks below are python chunks, we can set the engine globally:
```{r setup, include=FALSE}
opts_chunk$set(engine = 'python')
```
## Chunk Options
You can use some chunk options like `eval`, `echo` and `results`. For example, `eval=FALSE` (do not evaluate code):
```{r test-eval, eval=FALSE, ref.label='test-python'}
```
or `echo=FALSE` (hide source code):
```{r test-echo, echo=FALSE, ref.label='test-python'}
```
or `results='hide'`:
```{r test-hide, results='hide', ref.label='test-python'}
```
or `results='asis'`:
```{r test-asis, results='asis'}
print('**Write** _something_ in `Markdown` from `Python`!')
```
You can also cache the computation:
```{r test-cache, cache=TRUE}
import time
# pretend this is a time-consuming task...
time.sleep(10)
print(1+1)
```
## Strict Markdown
You can use strict markdown (i.e. indent by 4 spaces) by setting `render_markdown(TRUE)`.
```{r strict-markdown, engine='R'}
render_markdown(TRUE)
```
Now see how the output is changed:
```{r test-py-again, ref.label='test-python'}
```