I would like to convert an R Markdown notebook that contains both R and python chunks to an R script for execution on a backend server. We use a python pipeline to prepare the data. R code continues the analysis. The R markdown notebook comes from someone else and might be updated in the future. It would be nice if we can convert the notebook automatically to an R script. We don't necessarily need the notebook output, we are more interested in the data processing done in R chunks. And an R script is a little bit easier to use for debugging.
Input notebook analysis.Rmd
---
title: "The Ultimate Question"
---
```{r setup}
library(reticulate)
```
```{python}
import pandas
df = pandas.DataFrame({'x':[2,3,7], 'y':['life','universe','everything']})
```
```{r}
str(py$df)
prod(py$df$x)
```
I tried converting it to .R
with
knitr::purl("analysis.Rmd")
But the resulting analysis.R
file simply comments out the python lines
## ----setup--------------------------------------------------------------------
library(reticulate)
## import pandas
## df = pandas.DataFrame({'x':[2,3,7], 'y':['life','universe','everything']})
## -----------------------------------------------------------------------------
str(py$df)
prod(py$df$x)
Expected result
## ----setup--------------------------------------------------------------------
library(reticulate)
py_run_string("import pandas")
py_run_string("df = pandas.DataFrame({'x':[2,3,7], 'y':['life','universe','everything']})")
## -----------------------------------------------------------------------------
str(py$df)
prod(py$df$x)