Skip to content

Instantly share code, notes, and snippets.

@nischalshrestha
Last active June 3, 2020 19:36
Show Gist options
  • Save nischalshrestha/212038a4c0db13bc1f3f1246ec4c5254 to your computer and use it in GitHub Desktop.
Save nischalshrestha/212038a4c0db13bc1f3f1246ec4c5254 to your computer and use it in GitHub Desktop.
Python learnr
---
title: "Python exercises"
author: "Nischal Shrestha"
# output: library(devtools); library(here); load_all(here()); learnr::tutorial # use this when using a local learnr
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
# R related
library(reticulate)
# lesson related
library(learnr)
library(gradethis)
library(tidyverse)
library(ggplot2)
#
# # Reticulate setup for Python
virtualenv_dir = Sys.getenv('VIRTUALENV_NAME')
python_path = Sys.getenv('PYTHON_PATH')
dependencies = c('numpy', 'pandas')
reticulate::virtualenv_create(envname = virtualenv_dir, python = python_path)
reticulate::virtualenv_install(virtualenv_dir, packages = dependencies)
reticulate::use_virtualenv(virtualenv_dir, required = TRUE)
# knitr config
knitr::opts_chunk$set(echo = FALSE)
# learnr config
tutorial_options(exercise.timelimit = 30, exercise.checker = gradethis::grade_learnr)
```
Let's first import the `mpg` dataset from R. We can do so with `r.mpg`:
```{python data, exercise = TRUE}
df = r.mpg
df
```
<!-- Problem: this is not becoming accessible for python exercise chunks yet -->
<!-- ```{python} -->
<!-- df = r.mpg -->
<!-- df -->
<!-- ``` -->
<!-- Problem: currently no way to install python packages up on shinyapps.io server so df will be a dict. -->
Let's look at a summary of the DataFrame with pandas `info` function:
```{python inspect, exercise = TRUE}
df.info()
```
Now, let's only look at cars with more than 25 miles per gallon on the highway (`hwy`).
```{python wrangle, exercise = TRUE}
df = df[df.hwy > 25]
df
```
Let's make a scatter plot for the filtered data `df` in R using `ggplot2`!
We need to first access the pandas DataFrame `df` from R using `py$df` before making the plot:
```{r plot_py_data, exercise = TRUE, exercise.lines = 5}
df <- py$df
ggplot(data = df) +
geom_point(mapping = aes(x = displ, y = hwy))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment