generated from CUB-Computational-Tools/assignment_problem_set
/
example_python_in_rstudio.Rmd
executable file
·90 lines (71 loc) · 2.06 KB
/
example_python_in_rstudio.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
---
title: "Example: python in RStudio"
date: "Last knitted `r format(Sys.Date(), '%d %b %Y')`"
output:
html_document:
df_print: paged
number_sections: yes
toc: yes
toc_float: true
toc_depth: 3
code_folding: show
editor_options:
chunk_output_type: inline
---
> Click into a code cell (the gray blocks below) to select or edit it. To run a selected code cell, hit `Ctrl`+`Shift`+`Enter` (Windows & Linux) / `Command`+`Shift`+`Enter` (MacOS). Make sure that each code cell runs successfully before you move on to the next one.
```{r, include = FALSE, message = FALSE}
# required behind the scenes to find the correct python installation
library(reticulate)
# include the following command to activate a specific conda environment:
#use_condaenv(condaenv = "my_env", required = TRUE)
# note that this may get stuck knitting automatically in RStudio but will work
# if you knit manually using the following command from the console:
#rmarkdown::render("example_python_in_rstudio.Rmd")
```
# $\LaTeX$ Math
This is just markdown that can include latex math.
$$
\begin{align}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{align}
$$
# System Info
```{python}
# load IPython library for comprehensive system info
import IPython
print(IPython.sys_info())
```
# Custom Function
```{python}
# custom function from scripts/functions.py
from scripts.functions import test_add
test_add(2,5)
```
# Data
```{python}
# create dataset using pandas and numpy
import pandas as pd
import numpy as np
curve = pd.DataFrame(np.arange(0.0, 2.0, 0.01), columns = list('t'))
curve['y'] = 1 + np.sin(2 * np.pi * curve['t'])
curve[1:10] # first 10 rows
```
```{r}
# sidenote: you can access all python variables in R
py$curve
```
# Plot
```{r, include = FALSE}
# make sure the correct display is used for matplotlib
matplotlib <- import("matplotlib", convert = TRUE)
matplotlib$use("Agg")
```
```{python}
# plot using pyplot
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [8, 6]
plt.plot(curve['t'], curve['y'])
plt.show()
```