I am back to R after few years of absence. I want to use the breakpoints that do work in the main script as intended but not when the breakpoints is in an exterior function.
I created this simple piece of code just to visualize the issue.
In main.R I have these lines:
source("adding_numbers.R")
total_number <- adding_numbers(1, 2)
total_number2 <- adding_numbers(3, 4)
total_number3 <- adding_numbers(5, 6)
print(c(total_number,total_number2, total_number3))
The adding_numbers function is in the file adding_numbers.R and contains :
adding_numbers <- function (number1, number2)
{
added_number <- number1 + number2
z <- added_number + number1
return(added_number)
}
In this post they do mention to source all files or use debug().
When I click on the button source, it does send the console in debug mode and works well when the breakpoints are in main.R, but when the breakpoints are in adding_numbers.R it does not stop at the breakpoints.
Using debug(adding_numbers) do make the code automatically enter the function, but again does not stop at specific breakpoints.
What am I missing here?
Thank you a lot.
I think calling source()
from main.R
is the problem. That replaces the definition of adding_numbers
that has the breakpoint set with a new copy of the function, and RStudio doesn’t have a chance to re-set the breakpoint before you call it.
So you should call source("adding_numbers.R")
first, then set a breakpoint, then execute your main code.
Alternatively, you could use the base function setBreakpoint("adding_numbers.R#3")
to set a breakpoint on line 3 of that file. This needs to happen after you source the file, and before you call the function.
4