2

I made this code and it is working but only in Linux.

import subprocess as sub
sub.Popen([r"Rscript","diccionari.R"])

Where "diccionari.R" is the name of my script in R. Error text message: System can't found the specific file. enter image description here

Can somebody help me and do that it works on windows please? Thank you.

userbio
  • 115
  • 3
  • 14
  • Why is it not working, what is the exact error message you get? Right now we are just as unsure of what goes wrong as you are. – Paul Hiemstra Nov 11 '13 at 11:32
  • what error are you geting ? – rags Nov 11 '13 at 11:36
  • reedited with the error I'm geting. But I don't understant why in linux it works if it's the same file name, directory, etc... – userbio Nov 11 '13 at 11:37
  • You have to give the full path to the .r script! (that would be my guess) – PascalVKooten Nov 11 '13 at 11:44
  • Check this:http://stackoverflow.com/questions/9531683/problems-using-subprocess-call-in-python-2-7-2-on-windows – rags Nov 11 '13 at 11:47
  • 1
    I have checked it, and if I add shell=True to my script it opends my file but don't run the script (it only opens R console). – userbio Nov 11 '13 at 12:02
  • with shell=True, it works for me it displays the output of the Rscript. Also, with out shell=True, it worked for me if the path to RScript is given with double backward slashes in the path (C:\\My\\PATH\\RScript.R) – rags Nov 11 '13 at 12:09
  • rags can you put the code your are using please? My is only opening R console. – userbio Nov 11 '13 at 12:19
  • My Try.py has ... sub.Popen([r"Rscript","D:\\Python34\\diccionari.R"]) and my diccionari.R has a<-1+2 ... print(a) .... I am using python 3.4 and R 3.0.2 – rags Nov 11 '13 at 12:23
  • in my case it isn't working, but it may be my foult, so thank you! – userbio Nov 11 '13 at 14:42

2 Answers2

1

You should specify where Rscriptis located i.e

import subprocess as sub    
cmd_line = [r"C:\\Program Files\\R\\R-3.6.0\\bin\\Rscript", "diccionari.R"]

sub.Popen(cmd_line)

watch for the \\ characters

Kots
  • 486
  • 1
  • 5
  • 21
0

You should probably try the slashes the other way around as how I said it earlier.

Using full path to the .r script (e.g. "C:/myfolder/diccionari.R") instead of just the script file, and using OS independent slashes.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160