4

I would like to have a R function that allows me to emulate the R console with the difference that expressions are evaluated in a different environment than the global environment. (I want to use it for an alternative debugging approach in R that allows to restore the local parameters with which a function was called and then to debug by simply pasting step by step the function code into the emulated R console). Here is a function that uses parse and eval and does the job halfway (the emulated console stops when pressing Escape):

my.console = function() {
  while(TRUE) {
    tryCatch({
      expr.out <- capture.output(eval(parse(prompt=": ")))
      if (length(expr.out)>0) {
        cat(expr.out,"\n")
      }
    }, error = function(e) {
      str = as.character(e)
      message(str)
    })
  }
}

console.env = new.env(parent=globalenv())
console.env$hello = "Hello World"
environment(my.console) <- console.env

my.console()

One can then evaluate simple expressions in the emulated console, e.g.

: 5*5
[1] 25
: hello
[1] "Hello World" 

The problem is that I cannot parse multiline code, e.g. when pasting from my script the beginning of an if statement, an error will be thrown.

: if (TRUE) {
Error in parse(prompt = ": "): 2:0: unexpected end of input

The R console realizes that more code will be followed in the next line and changes the prompt to +. I wonder if there is also a variant of the parse function that has similar behavior. So far my only idea would be a very dirty approach to search for the substring "unexpected end of input" in the error message and if it is found to continue parsing. But e.g. since some R error messages seem to depend on the language in which R runs, I am not really happy with such an approach. Has anybody an idea how I could write a better emulated R console that can parse multiline R code?

2 Answers 2

1

Why not try separating your code input and parse commands, e.g.,

s <- scan(what="list", multi.line=TRUE)
for(i in 1:length(s)){
    capture.output(eval(parse(text=s)[i]))
    # etc...
}
3
  • Thanks for the suggestion, but like in the R console I want that once a complete R expression is entered, it shall be immediately evaluated. To make scan stop, it seems as if one has to enter explicitly an empty line. Furthermore, it seems not straigthforward to evaluate multiline expressions, I get an error. So far, I will rather stay with my dirty method described above of checking the R eror message. May 4, 2013 at 8:44
  • Do you need to be able to enter an arbitrary number of lines? scan accepts an nlines argument so that you don't have to enter a empty line to stop scanning.
    – Thomas
    May 4, 2013 at 10:36
  • Setting nlines=1 can be helpful, but there would still be the problem to detect whether the first line is a start of a multiline expression and to parse the whole expression correctly May 5, 2013 at 10:22
0

If you want to interactively go step-by-step through your code, you can use the builtin function browser() with command n, see ?browser. Example use:

f <- function(x) {
  # invoke interactive debugging
  browser()

  # rest of the function - by pressing 'n' while in browser mode, the expressions
  # will be executed one after another and you are able to explore the
  # intermediate values of variables etc. (as in standard R promt)
  y <- x^2
  [...]
}

If this does not fit your needs, you might have a look at the source code of browser.

1
  • Thanks for the suggestion, but my goal is to provide an alternative method for debugging, which I implemented in the CRAN package restorepoint. The package vignette describes it in some detail and why I prefer it over break points set with browser(). So far the package seems to work with my dirty workaround... but it would have been nice if there were a cleaner way in R to emulate a custom console that parses multi-line expressions. May 4, 2013 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.