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:
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:
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 deselectedI 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:
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.
options(error=function()traceback(1))
and yoursessionInfo()
details? Looking at the detail of your explanation my sense is that you are getting different results because your environment has changed. In executing the code, you are creating new variables that change the behaviour of other code. That is my immediate supposition anyway. Hopefully, that will help point you in the right direction. – Bustamantetry_traceback.R
? Is it the two linessource("myfun.R"); mysum <- sum_2_nums(3,"hello")
? – Carpel