1

I code in python and my coworker codes in R. I am trying to call an Rscript file that he wrote to incorporate it into a python application. I am able to successfully call the Rscript using window's powershell, however, when using the exact same command with the python subprocess library [subprocess.run() /subprocess.call()/ subprocess.Popen()/ subprocess.check_output()]. The command which works in power shell is:

Rscript C:/Users/file/path/script_name.R

Rscript is a variable in my PATH environment which references my Rscript.exe file path. I have tried substituting this for the full file path but this does not change the result. In my python code the subprocess call looks like the following:

subprocess.check_output('Rscript C:/Users/file/path/script_name.R, shell=True)

Running this code gives the error message below in my python IDE. I find this unusual for three reasons. The first is that the program claims to halt execution multiple times but continues and encounters the same error multiple times. Second is that the packages 'ggplot2' and 'scales' are definitely imported, as the code works in both Rstudio and powershell without triggering this error. There is also a line the mentions a problem with 'rstudioapi', but this package does not appear anywhere in the file, and I am not using Rstudio. I am looking for either a way fix the subprocess call, or a way to open a powershell window from python and enter the working command into the terminal window. I've been stumped by this and I don't think I understand the subprocess functions.

Error in loadNamespace(name) : there is no package called 'rstudioapi'
Calls: ReqFunc ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Fatal error: cannot open file 'script_name.R': No such file or directory

Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in contrib.url(repos, "source") : 
  trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted
Error in contrib.url(repos, "source") : 
  trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted

ReqFunc is the name of a function in the script

  • 1
    Your could be running either Python or R in a different environment or version. In PowerShell that you can run Rscript, run `python C:\path\to\script_name.py`, assuming python` is in your PATH variable. – Parfait Aug 27 '21 at 23:31
  • The function mentions that some of the errors are coming from `source()` so it's likely that the script file is running code from other script files. Those files might reference "rstudioapi". They would need to be re-written from the command line. It also looks like code has `install.packages()` which is bad to have in a script you want to repeatedly run from the command line. Without any sort of [reproducible code](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) it's tough to really help. – MrFlick Aug 28 '21 at 01:21

1 Answers1

1

I found the answer to my problems. The first thing I discovered is that subprocess.check_output record the entire log of my python console so all the repeated "Execution halted" messages were different instance of my activity while trying to debug my code. 2nd, the call to run the Rscript as a sub process failed because by default, it was calling Command Prompt and not Powershell. This was fixed using the following line of code.

subprocess.check_output(["powershell.exe",command])
  • Very odd as `Rscript` should work the same in CMD.exe or PowerShell.exe. Would have been nice to got an answer to above question. PowerShell may be running in a different environment. – Parfait Aug 31 '21 at 15:40
  • before I fixed the problem, running the python script from powershell in the same environment produced similar errors. – potatopowered Sep 01 '21 at 00:08