Loading pickle in R

Viewed 3569

I am trying to load a pickle object in R, using the following process found online.

First, I create a Python file called: "pickle_reader.py":

import pandas as pd
def read_pickle_file(file):
    pickle_data = pd.read_pickle(file)
    return pickle_data

Then, I run the following R code:

install.packages('reticulate')

require("reticulate")

source_python("pickle_reader.py")
pickle_data <- read_pickle_file("pathname") 

but I get an error that says:

Error in py_run_file_impl(file, local, convert) : 
  ImportError: No module named pandas

N.B. I tried installing pandas again but this doesn't change the issue.

Do you know how should I proceed?

Thank you in advance

3 Answers

I find this to be a much more straight forward method than making a new .py file. In an R code chunk do:

library(reticulate)

pd <- import("pandas")

x <- pd$read_pickle("file.pickle")

If you want to insert a python package into a different environment, which in this case is R, you must search how to install python packages in R. In this case, looking at the CRAN webpage you can see that in order to install pandas in the environment of R you need the command py_install('pandas').

Hope it helps!

Make sure that pandas installed. I suggest using conda environment. I read the pickle applying below steps:

  • Create conda environment and install necessary packages.

  • Then in R, you can set the right python (which is python in your conda env)

    Sys.setenv(RETICULATE_PYTHON = "~/anaconda3/envs/your_env/bin/python")

    library(reticulate)

You can check with py_config()

  • Now you can read your pickle files in R,
    
    loadData = function(file_path){
      require("reticulate")
      source_python("pickle_reader.py")
      
      pd <- import("pandas")
      return (pd$read_pickle(file_path))
    }

    features = loadData(features_path)
Related