Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erratic behaviour of traceback() with Rstudio: different output every time

I use RStudio; I have script1 importing script2 with source ; a function in script2 causes an error. The first time it happens, Rstudio tells me that script 1 at row x caused an error in script2 at row y. If I rerun it, it only tells me about the error in script2. Why? This can make debugging much more painful than it needs to be.

More in detail:

I have a myfun.R file which contains this function (of course this is just a toy example):

sum_2_nums <- function(x,y) {
  out <- x + y
  return(out)
}

I then import the function into another file using source:

source("myfun.R")
mysum <- sum_2_nums(3,"hello")

RStudio is set to debug -> on error -> error inspector. When I run the code above, I see:

enter image description here

which tells me that an error in myfun.R, row 12 was caused by try_traceback.R, row 13. Great.

However, if I run the script again, I get:

enter image description here

i.e. the error no longer gets traced back to try_traceback.R. I need to type traceback() in the console to see that. Why? The different behaviour the second time really puzzles me. This makes debug needlessly more painful than it needs to be! Is there a way to avoid it?

Note this questions is not a duplicate of this: they may look similar but the answer given there of using echo=TRUE or verbose=TRUE does not address my point about tracing the error to the first .R file.

I saw the same question here, but it remains unanswered.

Edit

To clarify, in answer to some comments:

  • like I said, if I click on Debug -> on Error -> I see that "error inspector" is ticked.

  • if I type options(error=function()traceback(1)) in the console, nothing happens on screen, but "error inspector" becomes deselected

  • I do not have anything else in my code. It is a toy example with only the lines I have shown, and nothing else. I do not know what else I can clarify - any guidance would be most appreciated.

  • I am very new to R. I use Python for data analysis but am curious to understand about R. I had heard that debugging was much more cumbersome in R and wanted to try for myself. Let's say I am not going to invest a lot of my time learning R if even only tracing back such a banal error is so problematic, so I'd like to understand if I am doing something wrong, or debugging and tracing back are always like this in R

  • when I say "run" I mean I click the "source" button in RStudio (the button next to "run"

  • sessionInfo() shows:

sessionInfo() R version 3.5.3 (2019-03-11) Platform:
x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale: [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252    

attached base packages: [1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.3 tools_3.5.3    yaml_2.2.0

Edit #2 (in reply to duckmayr's answer)

Let's maybe take a step back. In Python I am used to always seeing a rather detailed traceback of what called what when there is an error. E.g. when I see something like this, I find it very helpful:

enter image description here

The first screenshot I posted of R studio is quite similar, and similarly helpful. So my expected output would be to get the full traceback every single time the error occurs.

Why? Because that's most helpful to me; what would possibly be the advantages of NOT showing it every time? I don't get it.

Are you instead saying that it is by design that the full traceback is not listed every time? If so:

  • Why? Is there a fundamental reason I am missing? Is there any scenario where this makes more sense than what I am used to with Python?
  • Is this documented anywhere? I couldn't find it.
  • Is there a way to get the full traceback every single time? A setting to change? Would exiting and reopening RStudio do?

Yes, I understand I can always type traceback(), but Python's behaviour of showing the full traceback every single time is much more convenient - I genuinely fail to see what the upside of showing the traceback only the first time would be.

like image 587
Pythonista anonymous Avatar asked Apr 04 '19 21:04

Pythonista anonymous


1 Answers

I find it somewhat difficult to get precisely what behavior you are wanting RStudio to give you, but it may help you if I post some information about what is expected.

  • The full traceback is not listed every time: In my experience, RStudio only automatically prints the full traceback the first time a function is executed in its current form. If you change your code, then the very next call should also automatically print the full traceback. This behavior may or may not be automatically configurable. However, more importantly:
  • The R functions traceback() and debug() are always available to you, regardless of RStudio's settings! So, the first time I source your script, I see:

enter image description here

Then if I source your script with nothing else changed, I get:

> source('~/try_traceback.R')
Error in x + y : non-numeric argument to binary operator

But, this is no problem; I can just run:

> traceback()
5: sum_2_nums(3, "hello") at try_traceback.R#2
4: eval(ei, envir)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("~/try_traceback.R")

I could also use debug(sum_2_nums) to re-run the function with debug.

Additionally, if I change sum_2_nums() in myfun.R to the following:

sum_2_nums <- function(x,y) {
    cat("")
    out <- x + y
    return(out)
}

I again see

enter image description here

when I try to source your script.

So you may disagree, but I do not find debugging to be difficult in R, just remember the functions traceback() and debug() and you'll be alright.

like image 50
duckmayr Avatar answered Nov 15 '22 05:11

duckmayr