Search code examples
pythonrreticulate

Python script to read csv-file from `inst` folder within custom R package


I am building a R package that uses a Python script which in turn loads internal data. Both the py-script (load_csv.py) as well as the data (data.csv) are located in the inst/ folder of the package directory.

import pandas as pd
my_df = pd.read_csv("inst/data.csv")

The idea is to run the py-script when a R-function (rfunction) is called:

rfunction <- function() {
          reticulate::py_run_file(system.file("load_csv.py", package = "mypkg"))
}

After building the package and calling the r-function rfunction() I get the following error:

Error: FileNotFoundError: [Errno 2] No such file or directory: 'inst/data.csv'

How can I resolve this error? Is there maybe a system call that I can place within the py-script (analogous to system.file in R)?


Solution

  • I don't know much about R, but I'm guessing this is caused by the python script not being executed in the expected directory.

    I'm assuming your python file is located in the inst folder of your package. As a quick and dirty solution, you could try to get around this by modifying your script to something like

    import os
    import pandas as pd 
    
    datafile = os.sep.join((
        os.path.dirname(__file__), # this gives the directory in which the python file is located, i.e. <package>/inst/
        "data.csv"
    ))
    
    my_df = pd.read_csv(datafile)