3

I'm used to R, but new to R markdown. I would like to insert python chunks in the code. However, I cannot get it to work. Every time I press "Knit", it starts knitting until it hits the first python chuck. Then it just pauses, without providing an error message.

I've tried using many example codes setting up a python engine with reticulate and knitr from the internet, but none of them work. I've specified different python files, versions, etc. It seems like I'm missing something, but I cannot figure out what it is.

The code I've used:

---
title: "Test3"
author: "Stanny"
date: "11 september 2019"
output: pdf_document
#
---

## Set-up

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
use_python("C:\\Users\\STANNY~1\\OneDrive\\envs\\wiki\\python.exe")
```

## A normal R code chunk

```{r}
library(reticulate)
x = 42
print(x)
```

## A Python chunk

```{python}
x = 42 * 2
print(x) 
```

## Modify a Python variable

```{python}
x = x + 18 
print(x)
```

The output:

processing file: test3.Rmd
  |........                                                         |  12%
  ordinary text without R code

  |................                                                 |  25%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |........................                                         |  38%
  ordinary text without R code

  |................................                                 |  50%
label: unnamed-chunk-1
  |.........................................                        |  62%
  ordinary text without R code

  |.................................................                |  75%
label: unnamed-chunk-2 (with options) 
List of 1
 $ engine: chr "python"
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
S. Goffin
  • 113
  • 1
  • 5
  • Hi S. Goffin, I've run into the same problem when trying to knit to html. Have you ever figured out a solution? A hint would be much appreciated. – mabe Dec 11 '20 at 09:14
  • Hi! Are you using flexdashboard? I think you have to look up whether your output method is compatible with the python engine. I eventually gave up this quest and wrote everything in R. Sorry I cannot give you a more helpful answer! Good luck! – S. Goffin Dec 15 '20 at 02:23
  • Hi, thanks for replying. I don't use flexdashboard. I ended up not using conda evironment, this did the trick in my case (pitty though...). – mabe Dec 15 '20 at 20:32

2 Answers2

2

I was having the same issue with frozen evaluation of a python chunk via reticulate when using RStudio's "Knit" shortcut. An explicit call to render provided a workaround, where input matches the .Rmd filename to knit.

rmarkdown::render(input = "r-reticulate.Rmd", output_format = "pdf_document")
# rmarkdown::render(input = "r-reticulate.Rmd", output_format = "html_document")

An additional Kill 9 error was giving me trouble outside of the RStudio console (e.g., terminal / make).

The following seems to have fixed both the kill 9 error and the Knit shortcut freezing as per LTLA

# From R console:
reticulate::use_condaenv("r-reticulate")
reticulate::py_install("nomkl")

Relevant versions:

> .rs.rVersionString()
[1] "4.0.2"
> rstudioapi::versionInfo()$version
[1] ‘1.4.1103’
> packageVersion("rmarkdown")
[1] ‘2.6’
> packageVersion("knitr")
[1] ‘1.30’
> packageVersion("reticulate")
[1] ‘1.18’
1

saddly is not possible to run python on flexdashboard :(
https://github.com/yihui/knitr/issues/1667

dzem
  • 26
  • 1