Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling R script from Python does not save log file in version 4

I am calling from python an R script that is very simple called R Code.R:

args <- commandArgs(TRUE)
print(args)
source(args[1])

setwd("<YOUR PATH>")
output <- head(mtcars, n = n)
write.table(output, "output.txt")

using the following script:

import subprocess

pth = "<YOUR PATH>"


subprocess.call(" ".join(["C:/R/R-3.6.0/bin/x64/R.exe", "-f", '"' + pth + '/R Code.R"', "--args", 
                '"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"', "2>&1"]))


subprocess.call(" ".join(["C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/R Code.R"', "--args", 
                '"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"', "2>&1"]))

where arguments.txt contain: n <- 10

The problem is that when I am using R-4.0.3 the log.txt file is not generating and I need to dump a log file because it is automatically looking for it in a posterior process I have.

When I am executing in CMD (Windows) the following command:

C:/R/R-4.0.3/bin/x64/R.exe -f "<YOUR PATH>/R Code.R" --args "<YOUR PATH>/arguments.txt" 1> "<YOUR PATH>/log.txt" 2>&1'

It does work perfectly, it is only when embedded in another software.

Also, I have tried without white space in the name and calling the scripts from root folder without having to specify the path. Any idea of why it doesn't work for R-4.* or even better, how to solve it?

Thank you!

PD: Thank you, Martin, for your tips and for making me formulate a better question

like image 430
karen Avatar asked Jan 25 '21 15:01

karen


2 Answers

Rhelp people got this solved, thank you, Duncan Murdoch!

Solution 1:

import os
pth = "<YOUR PATH>"
os.system(" ".join(["C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/RCode.R"', "--args", 
                '"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"']))

Solution 2:

import subprocess
pth = "<YOUR PATH>"
subprocess.call(" ".join(["1>", '"' + pth + '/log.txt"', "2>&1",
                          "C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/RCode.R"', "--args", 
                '"' + pth + '/arguments.txt"']), shell = True)
like image 86
karen Avatar answered Oct 25 '22 07:10

karen


well, in one case (3.6.0) you use R.exe , in the other (4.0.3) Rscript.exe. Both R and Rscript have existed for a long time, and they have always had slightly different behavior.

You really should not confuse them with each other (even though, on Windows, I see, they look like the same file .. they will not behave the same).

Ok, now you use R.exe for both. Just to find out more / see more where the problem may happen, can you try all of

  1. using a minimal reproducible example, i.e. one we can use directly ourselves, i.e., not using "<YOUR PATH>" (nor setwd(.))
  2. not using file names with a ' ' (space), i.e., e.g., use code.R
  3. calling this from a "terminal"/shell instead of as python subprocess ?

Last but not least: Yes, for R 4.0.0, a completely updated toolset ("brandnew toolchain", e.g. much newer C compiler) was used to build R for windows, "Rtools 4.0" or rtools40: https://cran.r-project.org/bin/windows/Rtools/ . So changes are expected but should typically only have been to the better, not the worse ..

like image 26
Martin Mächler Avatar answered Oct 25 '22 08:10

Martin Mächler