How to call a python script that requires command line arguments (that are not static) from Rstudio?

Viewed 335

I am trying to create a Shiny app where a user will choose a short string from a drop-down menu, that string will then be passed to a python script which will output some values which will be used in the shiny app.

Using reticulate's py_run_file function with the needed values hardcoded works great. However, using this:

py_run_file('test_script.py arg1')

gives this:

Error in py_run_file_impl(file, local, convert) :

Unable to open file 'test_script.py arg1' (does it exist?)

Several threads suggest using a system() call to run a .py script with command line arguments but I don't think that would be feasible for the goals because the argument needs to be able to change. Other threads have suggested creating a python file that calls the original python file using os.system() with arguments, but that also doesn't work for my situation.

Does anyone have any ideas?

Thanks

1 Answers

If anyone else is struggling with this problem: I found a workaround.

Instead of feeding an argument to the python script, I just create a R global environment variable then call it in the python script.

Had no idea you could reference R environment variables by calls such as r.RVar in the python script, similar to py$PythonVar when calling python variables in R scripts.

Related