Debugging practices, possible feature request

When debugging packages, I like to be able to browser() at a particular point, and then work freely in that environment - running, modifying, and re-running lines of code, etc. Rstudio hinders me in this by always trying to jump into any loop I run, and frequently changing which part of the file is shown (usually jumping to the top). Now, I guess these features have been added for a reason - can anyone suggest changes to my approach that will allow me to work similarly, but without having to skip through loops and constantly adjust the view location? Do I need to learn a new approach to debugging? Perhaps Rstudio could include a 'don't help me debug' option? Thanks!


You can change the default settings by going to Debug-On Error-Error Inspector.

I'd need to see an example of what code you are trying to debug, but I've found great success with debugging functions by putting a debug around my function, and then when I run my function from say purr::map_chr
I can inspect variables that are defined, and walk through the code to see what is not working properly.


#Now call the function via purr, and RStudio will go into interactive mode
purr::map_chr(vector_a, <my_func>)

Let me know if this helps or not.

ok, consider the following code then - I want to break into the function environment, then run the for loop and print the outcome to check it. I don't want any interference / jumping around from rstudio, but I cannot see any way to prevent this jumping around.


    for(n in 1:4){


Do you mean you want to view what is happening to the variable n in your code? When you go into debug mode, you can see the value of any variable defined inside your function, and iterate with the next key so you can see if something in your code is making it behave improper. If so, then the above instructions are meant to do just that.

If you just wanted to print the value of x and n at each iteration, then you would want to:

testf <- function(x){0
  for(n in 1:4){
    print(glue::glue("value of x is",{x}))
    print(glue::glue("value of n is",{n}))



A separate, but necessary question though, is what exactly are you trying to accomplish with your function that you have defined?

I mean I want to be able to do anything inside the function environment, just as if I was typing directly into the R console normally -- run new code from elsewhere, modify my script, etc. This is very helpful when I am working on complicated functions and I want to browser() at a specific point, then continue developing / debugging the function. So, in this case, I don't care what the function is doing (it's arbitrary rubbish), but at the point where I browser(), I want to be able to select the text of the for loop, hit cntrl-enter, and have the for loop run as normal, without a) browsing into some deeper section, and b) changing the script that rstudio is showing or the position in the current script.

I think it is necessary to understand what task you are trying to accomplish with the function above, as it's generally not advised to use for loops in R, and rather, to define a function, and apply that function via purrr or the apply family.

Do you have a more complicated example that might be more helpful?

The example highlights my point. If you'd like it more complicated, feel free to include as much code prior to the browser() line as you like...