-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpython.Rmd
66 lines (46 loc) · 922 Bytes
/
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
# Python code chunks in R Markdown
## A normal R code chunk
```{r}
library(reticulate)
x = 42
print(x)
```
## Modify an R variable
In the following chunk, the value of `x` on the right hand side
is `r x`, which was defined in the previous chunk.
```{r}
x = x + 12
print(x)
```
## A Python chunk
This works fine and as expected.
```{python}
x = 42 * 2
print(x)
```
The value of `x` in the Python session is `r py$x`.
It is not the same `x` as the one in R.
## Modify a Python variable
```{python}
x = x + 18
print(x)
```
Retrieve the value of `x` from the Python session again:
```{r}
py$x
```
Assign to a variable in the Python session from R:
```{r}
py$y = 1:5
```
See the value of `y` in the Python session:
```{python}
print(y)
```
## Python graphics
You can draw plots using the **matplotlib** package in Python.
```{python}
import matplotlib.pyplot as plt
plt.plot([0, 2, 1, 4])
plt.show()
```