2

I am helping to put together an ArcMap Toolbox, one component of which makes a system call to R's Rscript command-line executable. Everything works nicely, except that while the call to Rscript is being executed, a black Windows command line console opens up. (The console looks much like the one you get if you click the Windows Home button and then type cmd and press return.)

To reproduce what I'm seeing, run the following code in ArcMap's Python Console (I'm using ArcMap 10.5.1.), replacing the path in that first line with the path to Rscript on your own computer.

rscript = "C:/R/R-current/bin/Rscript.exe"
rscript = rscript + ' --vanilla -e'
rcall = rscript + """ "Sys.sleep(10)" """
rcall
## 'C:/R/R-3.1.2/bin/Rscript.exe --vanilla -e "Sys.sleep(10)" '

import subprocess
subprocess.call(rcall)

Is there any way to get this (or some related call to R) to run without ArcMap opening up the distracting Windows cmd console?

(It may be worth mentioning that this behavior seems to be specific to ArcMap's Python console. Running the same Python commands from other Python IDE's (e.g. Visual Studio Code, Spyder) executes the R commands without opening up a Windows cmd console.)

2

As shown at the link in @KirkKuykendall's comment above, the key here is to tweak the value returned by subprocess.STARTUPINFO() and to then pass that modified value along via the startupinfo= argument of a call to subprocess.call() (or subprocess.Popen()). The following code, in which only the last three lines differ from that in my question, works as desired:

rscript = "C:/R/R-current/bin/Rscript.exe"
rscript = rscript + ' --vanilla -e'
rcall = rscript + """ "Sys.sleep(10)" """
rcall
## 'C:/R/R-3.1.2/bin/Rscript.exe --vanilla -e "Sys.sleep(10)" '

import subprocess
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.call(rcall, startupinfo = startupinfo)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.