Search code examples
pythonrpandasr-markdownreticulate

`import pandas` in Rmarkdown: works in the console, but doesn't knit


I'm trying to write Python code in an Rmarkdown file in RStudio. Generally speaking it works fine. I can write code chunks and knit them perfectly. I can also import and use packages: anything using, say, numpy or math runs and knits well.

Despite being able to run and knit code chunks that use other packages, I can't seem to knit the file when it contains chunks that use pandas. I can run these chunks and obtain the expected output. A minimal example would be simply:

import pandas as pd

which returns the (expected) output:

Python 3.9.4 (/usr/local/bin/python3)
Reticulate 1.20.9000 REPL -- A Python interpreter in R.

(I can also run code chunks using pandas functions.)

The problem is that, when I knit, I get the error:

Error in py_call_impl(callable, dots$args, dots$keywords) : ModuleNotFoundError: No module named 'pandas' Detailed traceback: File "<string>", line 1, in <module> File "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/reticulate/python/rpytools/loader.py", line 44, in _import_hook level=level Calls: <Anonymous> ... py_capture_output -> force -> <Anonymous> -> py_call_impl Execution halted

I don't understand why I am able to run these code chunks, but not knit them.

Thank you for your help!


Solution

  • Does your Rmarkdown file look somewhat similar to this for a simple example?

    ---
    title: "Untitled"
    author: "User"
    date: "5/6/2021"
    output: html_document
    ---
    
        
    ```{r setup, include=FALSE}
    library(reticulate)
    ```
        
    ```{python}
    import pandas as pd
    iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
    iris
    ```
    

    And you still get error? sometimes you may need to reinstall pip packages, usually pip3 install pandas works, you can also add py_install("pandas") inside your r chunk under library(reticulate) to have it install pandas to help it render properly. Sometimes making simple examples help break the problem down into smaller chunks