Q:

Running and debugging a script and function in R

I am a newbie.  I've an R file ,containing a function with a parameter. 
I' d like to perform the two following distinct operations:

Run the function
Execute step by step and debug it

suppose to have a simple function in an R file
"exampleSum"<-
function(x,y){
 x<-x+1
 return(x+y)
}

Could you tell me which are R commands to perform what I asked. 

A:

You have to call debug(<functionName>) to step through a function.  The next time you call the function, the so-called browser environment will automatically be opened and list the content of your function.  You perform one step by pressing Return or with the n command (next).  A single step is one block (usually a single line) that will be listed before it's run.  To run the function to its end, press c (continue).  When you're in browser mode, you can use ls(), str(), class(), ... to inspect the objects.  Your example looks like this:
> exampleSum <- function(x, y) {
+   x <- x+1
+   return(x+y)
+ }

> debug(exampleSum)             # set debug flag
> exampleSum(1, 2)              # run function in browser mode
debugging in: exampleSum(1, 2)
debug: {
    x <- x + 1
    return(x + y)
}
Browse[2]> n                    # next step
debug: x <- x + 1
Browse[2]> ls()                 # show objects
[1] "x" "y"
Browse[2]> x                    # show x
[1] 1
Browse[2]> c                    # run function to end
exiting from: exampleSum(1, 2)
[1] 4

> undebug(exampleSum)           # remove debug flag

The last call to undebug(<functionName>) removes the debug flag from the function such that it will be run normally the next time it's called.