5

I'm debugging a model. The input data is prepared with a python script that calls an R script, through a python call to subprocess.check_call() (now replaced by pbs.Command()). This R script has various bugs. When an error happens in the R script, we can see the error message in the standard error, appearing above (or below) the python error. The problem is that the error message returned by R only returns a small part of the code where the error happens, without any line number. We then have to search for that piece of code in the original source. It would be easier if we had the actual line number.

A related answer suggests using an IDE to run through the whole R code line by line again, but we'd like to know the line number where this error happens so we can at least run all the R code until that line.

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110

1 Answers1

3

There are two ways to run R, either in interactive mode, or in non-interactive mode.

If you start R in interactive mode, you will be able to explore the current environment and the state of the variables when your script hits an error. But you will not be able to provide a file to run or pass arguments to it from the command line. You will have to painfully do something like this, manually, every time:

$ R
> argv = c('a', 'b', 'c')
> source('/path/to/script.R')

If you provide in any way the file to run from the command line, you will start R in non-interactive mode. This is the case for all the following commands:

$ R /path/to/script.R a b c
$ R < /path/to/script.R
$ R -e "argv = c('a', 'b', 'c'); source('path/to/script.R')"
$ R CMD BATCH /path/to/script.R a b c
$ Rscript --vanilla /path/to/script.R a b c

In non-interactive mode when you hit an error, you will not got a traceback. You will not even get the name of script that produced the error nor the line number. It's hard to view R as a true programming language when it's in such a sorry state. It's more of a hobbyist's experiment.

The conclusion I draw from this, is that the R language is not made to be used in anything automatized, and should not be employed if you are building a pipeline. Because you won't be able to debug it. R should certainly not be used for making scripts that can take arguments from the command line. R is good solely at interactive, exploratory data analysis and visualization.

xApple
  • 6,150
  • 9
  • 48
  • 49