[R] Error when calling (R 4.0.x on Windows) from Python
Bill Dunlap
w||||@mwdun|@p @end|ng |rom gm@||@com
Wed Jan 27 19:50:42 CET 2021
I mistyped, 3.6.3 omits "1>", "log.txt" (not arguments.txt"), etc. My
test was run in C:\tmp and used -e instead of -f and bin/R.exe instead
of bin/x64/R.exe, using the following python script:
import subprocess
cmd363 = " ".join(["C:/R/R-3.6.3/bin/R.exe", "--vanilla", "--quiet",
# "-f", '"code.R"',
"-e", '"commandArgs()"',
"--args", '"' + 'arguments.txt"',"1>", '"' + 'log363.txt"', "2>&1"])
print(cmd363)
status363 = subprocess.call(cmd363)
cmd403 = " ".join(["C:/R/R-4.0.3/bin/R.exe", "--vanilla", "--quiet",
# "-f", '"code.R"',
"-e", '"commandArgs()"',
"--args", '"' + 'arguments.txt"',"1>", '"' + 'log403.txt"', "2>&1"])
print(cmd403)
status403 = subprocess.call(cmd403)
This gave
>>> import subprocess
>>>
>>> cmd363 = " ".join(["C:/R/R-3.6.3/bin/R.exe", "--vanilla", "--quiet",
... # "-f", '"code.R"',
... "-e", '"commandArgs()"',
... "--args", '"' + 'arguments.txt"',"1>", '"' +
'log363.txt"', "2>&1"])
>>> print(cmd363)
C:/R/R-3.6.3/bin/R.exe --vanilla --quiet -e "commandArgs()" --args
"arguments.txt" 1> "log363.txt" 2>&1
>>> status363 = subprocess.call(cmd363)
>>>
>>> cmd403 = " ".join(["C:/R/R-4.0.3/bin/R.exe", "--vanilla", "--quiet",
... # "-f", '"code.R"',
... "-e", '"commandArgs()"',
... "--args", '"' + 'arguments.txt"',"1>", '"' +
'log403.txt"', "2>&1"])
>>> print(cmd403)
C:/R/R-4.0.3/bin/R.exe --vanilla --quiet -e "commandArgs()" --args
"arguments.txt" 1> "log403.txt" 2>&1
>>> status403 = subprocess.call(cmd403)
> commandArgs()
[1] "C:\\R\\R-4.0.3/bin/x64/Rterm.exe" "--vanilla"
[3] "--quiet" "-e"
[5] "commandArgs()" "--args"
[7] "arguments.txt" "1>"
[9] "log403.txt" "2>&1"
After exiting python I log403.txt does not exist and log363.txt shows
the command arguments that R got:
C:\R\R-3.6.3>cat log403.txt
cat: log403.txt: No such file or directory
C:\R\R-3.6.3>cat log363.txt
> commandArgs()
[1] "C:\\R\\R-3.6.3/bin/x64/Rterm.exe" "--vanilla"
[3] "--quiet" "-e"
[5] "commandArgs()" "--args"
[7] "arguments.txt"
On Wed, Jan 27, 2021 at 10:34 AM Bill Dunlap <williamwdunlap using gmail.com> wrote:
>
> Note that in R-3.6.3 commandArgs() does not include the arguments
> intended to be processed by the shell, "1>", "arguments.txt", etc.,
> but in R-4.0.3 it does include them. It is as though an R shell()
> command was replaced by a system() command so cmd.exe didn't get a
> chance to process the command line.
>
> -Bill
>
> On Wed, Jan 27, 2021 at 12:39 AM Martin Maechler
> <maechler using stat.math.ethz.ch> wrote:
> >
> > >>>>> Martin Maechler
> > >>>>> on Tue, 26 Jan 2021 12:37:58 +0100 writes:
> >
> > >>>>> Marcel Baumgartner
> > >>>>> on Tue, 26 Jan 2021 08:55:48 +0100 writes:
> >
> > >> Dear all, my colleague posted our issue on stackoverflow:
> >
> > >> Calling R script from Python does not save log file in
> > >> version 4 - Stack Overflow
> > >> [https://stackoverflow.com/questions/65887485/calling-r-script-from-python-does-not-save-log-file-in-version-4]
> >
> > >> It is about this kind of call to R:
> >
> > >> R.exe -f code.R --args "~/file.txt" 1> "~/log.txt" 2>&1".
> >
> > >> The issue is that the log.txt file is not created when
> > >> running R 4.x.x. The same code works perfectly fine with
> > >> R 3.6.x.
> >
> > >> Any idea what's going wrong as of version 4? Regards
> > >> Marcel
> >
> > > Dear Marcel, I think the solution is embarrassingly
> > > simple:
> >
> > >> From the SO post, where she showed a bit more detail than you
> > > show here, it's clear you have confused 'R.exe' and
> > > 'Rscript.exe' and what you say above is not true:
> >
> > > 'R.exe' was used for R 3.6.0 but for R 4.0.3, you/she used
> > > 'Rscript.exe' instead.
> >
> >
> > > ... as you've noticed now, they do behave differently,
> > > indeed!
> >
> > Well, this was not the solution to their -- Windows-only -- problem.
> > The problem *is* indeed visible if they only use R.exe (also
> > for R 4.0.3).
> >
> > I've commented more on the SO issue (see above),
> > notably asking for a *minimal* repr.ex. (reproducible example),
> > and one *not* using "<YOUR PATH>" and setwd() ..
> >
> > Martin
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list