Can not knit RMarkdown with Python code chunk inside

Hey there,

if I use python chunks in my RMarkdown file, I always get the error when I try to knit it:

Quitting from lines 12-22 (Test.Rmd) 
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: _repr_html_() missing 1 required positional argument: 'self'
Calls: <Anonymous> ... eng_python_autoprint -> as_r_value -> <Anonymous> -> py_call_impl
Execution halted

If I simply run the code, there are no problems.

---
title: "Untitled"
output: html_document
date: '2022-06-05'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{python}

import seaborn as sns

# Load an example dataset

tips = sns.load_dataset("tips")

# class is a pandas.core.frame.DataFrame

type(tips)
```

```{r}

# access with py$variable_name

tips <- py$tips

# class is a R data.frame

class(tips)
```



Versions:


platform       x86_64-w64-mingw32               
arch           x86_64                           
os             mingw32                          
crt            ucrt                             
system         x86_64, mingw32                  
status                                          
major          4                                
minor          2.0                              
year           2022                             
month          04                               
day            22                               
svn rev        82229                            
language       R                                
version.string R version 4.2.0 (2022-04-22 ucrt)
nickname       Vigorous Calisthenics

I can't reproduce this issue while running your code

---
title: "Untitled"
output: html_document
date: '2022-06-05'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
```

```{python}

import seaborn as sns

# Load an example dataset

tips = sns.load_dataset("tips")

# class is a pandas.core.frame.DataFrame

print(type(tips))
```

```{r}
# access with py$variable_name
tips <- py$tips
# class is a R data.frame
class(tips)
```

BTW I reformated your post - see FAQ: How to Format R Markdown Source

Thank you for the reformatting.

I am sorry, thats my exact error:

Quitting from lines 12-22 (Test.Rmd) 
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: _repr_html_() missing 1 required positional argument: 'self'
Calls: <Anonymous> ... eng_python_autoprint -> as_r_value -> <Anonymous> -> py_call_impl
Execution halted

Does it operate without any problems or do you have another error but not the same?

I think I had the same but I called print() explicitly here.

I believe this would be an issue in reticulate package where the python engine for knitr does not handle priting type() output correctly.

Can you try changing this ?

1 Like

Yes thats it.
Thank you so much :slight_smile:

I still think it is an issue in reticulate. Do you want to open an issue there to share that without print() the output is creating an error ?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.