Search code examples
rreticulate

R: Reticulate -- How to call a Python script that requires multiple command line arguments


I have a script called DataRetrieval.py. This script connects to an internal database, retrieves data and writes that data to an Excel file. This script imports the following:

a) a utils.py helper module that contains several functions

b) a DataRetrieval.cfg configuration file (which contains all of the login information and SQL queries that the DataRetrieval.py script needs)

The DataRetrieval.py script takes the following 3 command line arguments:

DataRetrieval.py   DataRetrieval.cfg   03/01/20

(the script itself, the configuration file, and a start date)

I would like to use the Reticulate package in R to:

  1. call each of these 3 command line arguments
  2. set the output of the script (which is an Excel file) to an object (the end goal is to call this object in a Shiny dashboard)

I was thinking about using the source_python() function, but I don't think it allows for multiple positional arguments. The error message returns:

> source_python("DataRetrieval.py")
Error in py_run_file_impl(file, local, convert) : SystemExit: 2
usage: python.exe [-h] [-e ENDDATE] config startDate
python.exe: error: the following arguments are required: config, startDate

What is the best way to use the Reticulate package to call these 3 command line arguments? Perhaps a single .py file that encapsulates each of the 3 arguments above?

Thanks!


Solution

  • EDIT: I solved this problem by creating a .py file named retrieve.py and passing that file to the source_python() Reticulate function. The retrieve.py file reads as follows:

    import os
    
    os.system('python DataRetrieval.py DataRetrieval.cfg 02/01/20')