0

I'm trying to automate a workflow with Python on a Mac, which involves calling R to generate image files.

Following the answer to this question, I'm using subprocess.Popen to try calling Rscript and execute my image-generating R file.

However, I'm not sure where Rscript would be located on my machine.

Here's code I borrowed from the referenced question, and the error it returns:

def make_plot():
command = '/usr/bin/Rscript'
arg = '--vanilla' 


try: 
     p = subprocess.Popen([command, arg,
                          "r_script_for_python.R"],
                          cwd = os.getcwd(),
                          stdin = subprocess.PIPE, 
                          stdout = subprocess.PIPE, 
                          stderr = subprocess.PIPE) 

     output, error = p.communicate() 

     if p.returncode == 0: 
        print('R OUTPUT:\n {0}'.format(output.decode("utf-8"))) 
     else: 
        print('R ERROR:\n {0}'.format(error.decode("utf-8"))) 

     return True

except Exception as e: 
     print(e)

     return False

Running make_plot() returns an error: No such file or directory: '#!/usr/bin/Rscript'

Thalecress
  • 3,231
  • 9
  • 35
  • 47

0 Answers0