Skip to content

Running Python chunks in learnr on shinyapps.io #821

@tetlabo

Description

@tetlabo

I’m trying to use the learnr package to publish multilingual tutorials on shinyapps.io.

I’ve run into an issue specifically with using Python in these tutorials:

  1. The Python path is correctly recognized on shinyapps.io.
  2. I can run Python code from R using the reticulate package.
  3. However, when using the Python engine in R Markdown chunks, it fails to run.

All chunks (including the Python ones) work perfectly when run locally using rmarkdown::run().

Here is a simplified version of the code I’m using:

---
title: "Python on learnr"
date: "`r format(Sys.time(),'%Y-%m-%d')`"
author: "tetlabo"
output: learnr::tutorial
runtime: shiny_prerendered
---

```{r setup, include = FALSE}
options(dplyr.width = Inf, scipen = 1, digits = 4)
library(reticulate)
# use_python(python = "/usr/bin/python3", required = TRUE) # Python path on shinyapps.io
```

## Introduction

This file tests running Python in a learnr tutorial on shinyapps.io.

```{bash code01, exercise=TRUE, eval=TRUE, message=FALSE, warning=FALSE}
which python3
```

The system recognizes Python.

```{r code02, exercise=TRUE, eval=TRUE, message=FALSE, warning=FALSE}
sys <- import("sys")
sys$version
```

Using reticulate works fine.

```{python code03, exercise=TRUE, eval=TRUE, message=FALSE, warning=FALSE}
import sys
print(sys.version)
```

However, using the Python engine in R Markdown results in an error.

Here is the actual deployed tutorial:
https://9z2nv0-0-0.shinyapps.io/00_tutorial/

The error message I get in the chunk is:

"An internal error occurred while setting up the exercise. Please try again or contact the tutorial author."

Unfortunately, this message is not informative enough to help me debug the problem.

Do you have any insight or suggestions on how to get Python chunks working in learnr tutorials deployed on shinyapps.io?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions