pythonrsubprocesspyinstallereel

Bundle R with a python exe application (using Pyinstaller)


I'm working on a python application using Eel framework and I'm using the python subprocess library to execute an R script. It works completely fine on my computer, but not in any other computer since it doesn't have R installed (and obviously R will not be in the PATH).

I'm wondering if there is a way to bundle the entire R with my python exe so its a completely independent application and doesn't require the client to have R installed + to have R in the PATH.

Here is where I call my R script from python:

rScript = resource_path("map.R")
retValue = subprocess.check_output(["Rscript", '--vanilla', rScript, finalDirectory],
                                   shell=True, universal_newlines=True)

Here is the pyinstaller command: python -m eel flight_checker.py web --icon=web\drone.png --add-data "map.R;."

I really appreciate any help provided.


Solution

  • Posting my solution in case anyone falls into the same issue:

    I ended up bundling the whole R by executing this pyinstallter command: python -m eel flight_checker.py web --add-data "map.R;." --add-data "C:\Program Files\R;." --icon=web\drone.ico --clean

    Notes:

    1. My path for R might be different than yours. Change accordingly.
    2. This will create a separate R interpreter with its own library path. Hence, it's crucial to specify the path of our library whenever installing\loading R packages (so it will be installed in your new created version of R, not in the main one that's saved in Program Files). To do this, at the top of your R script, simply put .libPaths("insertYourLibraryPath"). Your library path should look similar to this: C:\Users\CURRENT_USER\PATH_TO_YOUR_PROJECT\dist\PROJECT_NAME\R-4.2.1\library

    Hope that helps.