Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress warnings when using a python chunk inside an Rmd file

I am trying to hide some python warnings when knitting an Rmd file. The usual chunk setup "warning=F, message=F" doesn't seem to work for python chunks.

Example of Rmd file with a python chunk that, purposefully, generates warnings:

---
title: "**warnings test**"
output: pdf_document
---


```{python, echo=F, warning=F, message=F}
import pandas as pd
d = {'col1': [1, 1, 2, 2], 'col2': [0, 0, 1, 1]}
df = pd.DataFrame(data=d)
df[df.col1==1]['col2']=2
```
like image 553
Julien Massardier Avatar asked Sep 18 '19 14:09

Julien Massardier


People also ask

How do I show output in R Markdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .


1 Answers

You can add warnings.filterwarnings('ignore') to the python chunk:

```{python, echo=F, warning=F, message=F}
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
d = {'col1': [1, 1, 2, 2], 'col2': [0, 0, 1, 1]}
df = pd.DataFrame(data=d)
df[df.col1==1]['col2']=2
```
like image 59
Matt Avatar answered Sep 26 '22 06:09

Matt