Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running and debugging a script and function in R

Tags:

r

debugging

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:

  1. Run the function
  2. 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.

like image 628
elisa Avatar asked Dec 29 '10 05:12

elisa


People also ask

How do you debug a function in RStudio?

You can do this in RStudio by clicking to the left of the line number in the editor, or by pressing Shift+F9 with your cursor on the desired line. We call this an “editor breakpoint”. Editor breakpoints take effect immediately and don't require you to change your code (unlike browser() breakpoints, below).

How do you debug a function?

To debug a function which is defined inside another function, single-step through to the end of its definition, and then call debug on its name. If you want to debug a function not starting at the very beginning, use trace(..., at = *) or setBreakpoint .


2 Answers

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.

like image 172
caracal Avatar answered Oct 17 '22 15:10

caracal


You first need to load the function into R's workspace (by copy/pasting it to R). Then, you can run it using exampleSum(x = 1, y = 1). You can check that it's there by typing exampleSum into R console. You can also run the script file using source(). Example of usage would be source("d:/R/my_script.R").

I think you would benefit immensely by reading at least An Introduction to R. There is also a plethora of books available to R beginners that explain the very basics. Equivalent information is available in the aforementioned AI2R and free materials floating around the internet. Searching the R help list is also... helpful.

like image 20
Roman Luštrik Avatar answered Oct 17 '22 14:10

Roman Luštrik