To use Python in R Markdown, we need to load the 
{[reticulate](https://rstudio.github.io/reticulate/)} R package in a 
code chunk, we do it here in the setup code chunk. We also load the 
{[ggplot2](https://ggplot2.tidyverse.org/)} R package so we can plot some data 
from a Python `pandas` data frame later on!

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
library(ggplot2)
```

## Python in `.Rmd` files

### Load libraries

Just like R, you can use Python in `.Rmd` files! Here we import the Python 
package that we need to import our data into Python, `pandas`. `pandas` is a
Python package that adds data reading, wrangling, and simple data visualization 
functionality to Python. It holds a similar place as the {tidyverse} R 
meta-package does (however `pandas` is not a meta-package, just a very large
package). If you want to learn more about pandas, see the 
"[10 minutes to pandas](https://pandas.pydata.org/pandas-docs/stable/user_guide/10min.html)"
in the docs our checkout this free interactive course: <https://prog-learn.mds.ubc.ca/en>.

```{python libraries}
import pandas as pd
```

### Load data

Let's load the titanic data (which lives in the `data` directory of this 
project) and view the data:

```{python data}
titanic  = pd.read_csv("data/titanic.csv")
```

### plot data

#### Using Python and `pandas`
First, let's use some quick and simple `pandas` plotting functions:

```{python python-plot}
titanic.plot.scatter(x='age', y='fare', alpha=0.3)
```

Want to learn more about getting started plotting in Python using `pandas`, 
see this page to get started: <https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html>

#### Using R and {ggplot2}

We can also access the Python environment in this R Markdown document from R! 
This allows us to apply R's functions on our Python objects! We can use the 
`py$obbject` syntax to do this and create a data visualization using {ggplot2}!

```{r r-plot, warning=FALSE, fig.height=4, fig.width=4}
ggplot2::ggplot(py$titanic, aes(x = age, y = fare)) +
  geom_point()
```



### Subsetting data in `pandas`

Here we subset the age and fare columns:

```{python}
titanic[["age", "fare"]]
```


Here we filter for rows containing passengers who's age was 70 or greater:

```{python}
titanic[titanic["age"] > 70]
```

Here we find the destination of the first passenger:

```{python first destination}
first_dest = titanic["home.dest"][0]
first_dest
```

Why 0 as the numerical index above? Python counts from 0, not 1!

### And inline python code within markdown text!

We can use the R Markdown inline syntax and the `py$` syntax to reach into the 
Python environment and access the `first_dest` python object (which is a string).

The destination of the first passenger is `r py$first_dest`.
