Reputation: 33
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
Upvotes: 1
Views: 399
Reputation: 33
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])
Upvotes: 1