1

Possible Duplicate:
General suggestions for debugging R?

When debugging, I would often like to know the value of a variable used in a function that has completed executing. How can that be achieved?

Example:

I have function:

MyFunction <- function() {
    x <- rnorm(10) # assign 10 random normal numbers to x
    return(mean(x))
}

I would like to know the values stored in x which are not available to me after the function is done executing and the function's environment is cleaned up.

Community
  • 1
  • 1
Dmitrii I.
  • 696
  • 7
  • 16

4 Answers4

4

You mentioned debugging, so I assume the values are not needed later in the script, you just want to check what is happening. In that case, what I always do is use browser:

MyFunction <- function() {
    browser()
    x <- rnorm(10) # assign 10 random normal numbers to x
    return(mean(x))
}

This drops you into an interactive console inside the scope of the function, allowing you to inspect what is happening inside.

For general information about debugging in R I suggest this SO post.

Community
  • 1
  • 1
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • To avoid having to modify the code, you can also use `debug(MyFunction)` or `debugonce(MyFunction)`. But these will all interrupt execution as the function is called. – flodel Oct 06 '12 at 11:52
2
MyFunction <- function() {
    x <- rnorm(10) # assign 10 random normal numbers to x
    return(list(x,mean(x)))
}

This will return a list where the first element is x and the second is its mean

chandler
  • 716
  • 8
  • 15
1

You have many options here. The easiest is to use the <<- operator when you assign to x. It's also the most likely to get you into trouble.

> test <- function() x <- runif(1)
> x <- NA
> test()
> x
[1] NA
> test <- function() x <<- runif(1)
> test()
> x
[1] 0.7753325

Edit

@PaulHeimstra points out that you'd like this for debugging. Here's a pointer to some general tricks:

General suggestions for debugging in R

I'd recommend either setting options(error=recover) or using trace() in combination with browser().

Community
  • 1
  • 1
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • 1
    The OP states `when debugging`, so probably the OP wants have the value to check if things are going ok. I would use `browser` in that case. – Paul Hiemstra Oct 06 '12 at 11:45
1

There are already some good solutions, I'd like to add one possibility. I emphasize on the fact that you want to know the value of a variable used in a function that has completed executing. So there is maybe no need to assign those values, and you don't want (a priori) to stop execution. The solution is to simply use print. So it is not used by default but only when you want to debug, the option to print or not can be passed as a function argument:

MyFunction <- function(x, y, verbose = FALSE) {
    a <- x * y
    if (verbose) print(a)
    b <- x - y
    if (verbose) print(b)
    return(a * b)
}

In general, you would run your function like this: MyFunction(10, 4) but when you want to see those intermediate results, do MyFunction(10, 4, verbose = TRUE).

flodel
  • 87,577
  • 21
  • 185
  • 223