Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python together with knitr

I would like to use python together with knitr. However, python chunks seem to get evaluated separately, and variable definitions are lost between chunks.

How to solve this?

Minimal example:

test.pymd

---
title: "Minimal example"
---

With a print statement.

```{r hello}
x = 'Hello, Python World!'
print(x)
```

Without a print statement.

```{r world}
print(x)
```

test.md

---
title: "Minimal example"
---

With a print statement.


```python
x = 'Hello, Python World!'
print(x)
```

```
Hello, Python World!
```

Without a print statement.


```python
print(x)
```

```
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'x' is not defined
```

knit2py.r

#! /usr/bin/Rscript --vanilla

args <- commandArgs(TRUE)

if(length(args) < 1) {
    message("Need arguments in the format: %.pymd [%.md]")
    q(status=1)
}

name <- substr(args[1],1,nchar(args[1])-4)

if(length(args) < 2) {
    args[2] <- paste0(name,".md")
}

library(knitr)
opts_chunk$set(engine = 'python')

res <- try(knit(args[1], args[2]))

if(inherits(res, "try-error")) {
    message("Could not successfully knit RMD (or PYMD) to MD")
    q(status=1)
} else q()

And now run:

./knit2py.r test.pymd test.md 
like image 250
Thomas Möbius Avatar asked Oct 21 '16 08:10

Thomas Möbius


People also ask

Can you use R Markdown with Python?

The reticulate package includes a Python engine for R Markdown that enables easy interoperability between Python and R chunks.

How do you use an R variable in Python?

In order to call Python variables from R you just need to access those variables within the py object. Basically, py is your entrance in R to your Python environment.

What is the purpose of knitr?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

How is Quarto different from R Markdown?

Quarto offers an Extension mechanism to add features to a format using Shortcodes or Filters but also create custom formats. A major difference with custom output format in R Markdown is that Quarto Extension does not use R but Lua, for example if you need to add some logic behind custom metatada fields.


1 Answers

Yes, indeed, knitr is currently not able to evaluate code stretching over multiple chunks for languages other than R. The solution is not to use knitr but to use pweave instead. The modifications to the source file are minimal:

test.mdw

---
title: "Minimal example"
---

With a print statement.

<<>>=
x = 'Hello, Python World!'
print(x)
@

Without a print statement.

<<>>=
print(x)
@

The end.

And now run:

pweave -f pandoc test.mdw

There is a note on the pweave's website that installation would fail with pip using python3. I had, though, no problems at all, when simply running:

pip install pweave
pip install markdown

Maybe, it is just an old note.

like image 73
Thomas Möbius Avatar answered Nov 15 '22 21:11

Thomas Möbius