How to Get RMarkdown to Ignore Python Error

I have an rmarkdown file with an intentional error in a python chunk. I want the statement evaluated, the error message returned and the rest of the documented knitted.

This does not work:

```{python error=TRUE}
dollar_tree_list = soup.find_all(class = 'itemlist')

I get the error message, but the knitting process stops. It doesn't matter if error=FALSE or if this option is applied to the global options. (Also, the final 3 backticks are in my code, but I'm having trouble showing that in this interface).

If it matters, I am knitting to ioslides just using the button in RStudio.

Any ideas?

Hi @jpiaskowski,

This works for me...

```{python, error=TRUE, include=TRUE}
import fakepackage
```

The code should proceed even if it errors.

I just tried that and it did not work - I still got the "invalid syntax....Execution halted" error (and also, the error message was suppressed, and I do need that for teaching). Thank for the tip, though. It was worth a try.

Can provide a reproducible example? For me, with error=TRUE and include=TRUE errors can happen and the error messages get printed. Are you sure it is this code chunk that is causing the issue?

Yes, I'm sure that is the line - it's there on purpose, it's the only line that fails (I've tested this file one chunk at time) and that exact numbered line is provided in the error message ("Line 145 Error: invalid syntax...."). I can work on a reprex.

In the meantime, can you share the exact error message?

Here's an example. The end of each chunk only has 2 backticks for this example only so it would render slightly closer to reality in this interface. The original code has 3 trailing backticks in each code chunk.

---
title: "Test"
author: "unknown"
date: "2020/0114"
output:
  slidy_presentation: default
  ioslides_presentation: default
---

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

```{r echo=F}
library(reticulate)
virtualenv_create("test")
use_virtualenv("test")
``

## Slide 1

```{python}
import requests 
from bs4 import BeautifulSoup
import json 
from pandas import DataFrame as df 
``

## Slide 2

Totally okay error
```{python error=TRUE}
print(A)
``

## Slide 2

```{python}
page = requests.get("https://locations.familydollar.com/id/")
soup = BeautifulSoup(page.text, 'html.parser') 
``

## Slide 3

not okay error 
```{python error=TRUE}
dollar_tree_list = soup.find_all(class = 'itemlist')
``

## Slide 4

[empty]



the error message is thus;

Line 46 Error: invalid syntax (<string>, line 1) Execution halted

"Line 46" works in this reprex, and it is different in my original file.

I don't yet know why its happening, but removing/changing the word "class" in dollar_tree_list = soup.find_all(class = 'itemlist') gets it to render for me...hmm. In fact the word class turns blue, and if I replace it with another protected word like for it also fails to render.

Okay, thanks for your efforts. It is a purposely written line of erroneous to highlight a common syntax error.

I'll alert the rmarkdown package author as it does look like a bug. And for my presentation, I will rely on a screenshot of that code and error message, instead.

Your chunks need three ticks on the top and bottom:

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

I get your error when only using two for the bottom. It's not a Python error, it's a knitr error.

I believe the 2 trailing back-ticks were just for when they presented the example code here on the forums. I reproduced their error with the proper code chunk delimiters. The document still fails to render using the following code:

---
title: "Test"
author: "unknown"
date: "2020/0114"
output:
  slidy_presentation: default
  ioslides_presentation: default
---

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

## Slide 1

```{python}
import requests 
from bs4 import BeautifulSoup
import json 
from pandas import DataFrame as df 
```

## Slide 2

Totally okay error
```{python error=TRUE}
print(A)
```

## Slide 2

```{python}
page = requests.get("https://locations.familydollar.com/id/")
soup = BeautifulSoup(page.text, 'html.parser') 
```

## Slide 3

not okay error 
```{python error=TRUE}
dollar_tree_list = soup.find_all(class = 'itemlist')
```

## Slide 4

[empty]
1 Like

Yes, I know. As indicated above, the 2 backticks are an imperfect hack for this particular interface. If you have another solution to improve the display - such as how to escape backticks in a markdown documents - please let me know.

You can wrap the code in 4 back-ticks here on the forum. This allows you to have back-ticks inside the code block. Example (this is wrapped in 4 back-ticks):

```{r}
rnorm(100)
```
1 Like