Re: [R] traceback performs differently in "interactive-mode" then in "script-mode"

2022-12-21 Thread Yihui Xie
Hi Malte,

I think I asked the same question on Stack Overflow ten years ago:
https://stackoverflow.com/q/13116099/559676 I hope you'll find the answer
helpful there.

Regards,
Yihui
--
https://yihui.org


On Wed, Dec 21, 2022 at 6:19 AM Flender, Malte 
wrote:

> Hello,
>
> a few days ago I encountered a strange behavior of base R.
> I'm not really sure if it is a bug or not.
> Thus I am somewhat hesitant to write a bug report.
> Instead I write to R-Help to ask you if this behavior can be considered a
> bug or not.
> I started with a question at stackoverflow (
> https://stackoverflow.com/questions/74796994/r-traceback-performs-differently-in-interactive-mode-then-in-script-mode),
> but got no response there.
>
> The strange behavior of base R lies in how it acts differently in an
> interactive R-Session then the execution of an R-Script.
> As you see in the MWE below the traceback()-function returns NULL in
> script mode (first call) and an actual traceback in interactive mode
> (second call).
>
> Can you reproduce this behavior?
>
> Here is MWE, which contains a small test-script and the calls to it in an
> interactive and script mode:
>
> user@server:~/folder> cat test.r
>
> options(error = NULL)
>
> onexit <- function() {
> error.msg <- geterrmessage()
> traceback <- traceback()
>
> print(paste("error.msg: ", nchar(error.msg), sep = ""))
> print(paste("traceback: ", is.null(traceback), " : ", traceback, sep =
> ""))
>
> if (nchar(error.msg) != 0 && !is.null(traceback)) {
>   print("Uncaught Error")
> } else if (nchar(error.msg) != 0 && is.null(traceback)) {
>  print("Caught Error")
> } else if (nchar(error.msg) == 0 && is.null(traceback)) {
>  print("No Error")
> } else {
> stop("ERROR in on.exit: bad traceback error.msg combination")
> }
> }
>
> fail <- function() {
>   on.exit(
>   onexit(),
>   add = TRUE,
>   after = TRUE)
>
>   print("SOMETHING")
>   stop("BAD")
> }
>
> fail()
>
> user@server:~/folder> docker run -it --rm -v /home/user/folder/:/data/R/
> r-base:4.2.2 R -e 'source("/data/R/test.r")'
>
> R version 4.2.2 (2022-10-31) -- "Innocent and Trusting"
> Copyright (C) 2022 The R Foundation for Statistical Computing
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under certain conditions.
> Type 'license()' or 'licence()' for distribution details.
>
>   Natural language support but running in an English locale
>
> R is a collaborative project with many contributors.
> Type 'contributors()' for more information and
> 'citation()' on how to cite R or R packages in publications.
>
> Type 'demo()' for some demos, 'help()' for on-line help, or
> 'help.start()' for an HTML browser interface to help.
> Type 'q()' to quit R.
>
> > source("/data/R/test.r")
> [1] "SOMETHING"
> Error in fail() : BAD
> Calls: source -> withVisible -> eval -> eval -> fail
> No traceback available
> [1] "error.msg: 75"
> [1] "traceback: TRUE : "
> [1] "Caught Error"
> Execution halted
> user@server:~/folder> docker run -it --rm -v /home/user/folder/:/data/R/
> r-base:4.2.2 R
>
> R version 4.2.2 (2022-10-31) -- "Innocent and Trusting"
> Copyright (C) 2022 The R Foundation for Statistical Computing
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under certain conditions.
> Type 'license()' or 'licence()' for distribution details.
>
>   Natural language support but running in an English locale
>
> R is a collaborative project with many contributors.
> Type 'contributors()' for more information and
> 'citation()' on how to cite R or R packages in publications.
>
> Type 'demo()' for some demos, 'help()' for on-line help, or
> 'help.start()' for an HTML browser interface to help.
> Type 'q()' to quit R.
>
> > source("/data/R/test.r")
> [1] "SOMETHING"
> Error in fail() : BAD
> 6: stop("BAD") at test.r#30
> 5: fail() at test.r#33
> 4: eval(ei, envir)
> 3: eval(ei, envir)
> 2: withVisible(eval(ei, envir))
> 1: source("/data/R/test.r")
> [1] "error.msg: 22"
> [1] "traceback: FALSE : stop(\"BAD\")"
> [2] "traceback: FALSE : fail()"
> [3] "traceback: FALS

[R] traceback performs differently in "interactive-mode" then in "script-mode"

2022-12-21 Thread Flender, Malte
Hello,

a few days ago I encountered a strange behavior of base R.
I'm not really sure if it is a bug or not.
Thus I am somewhat hesitant to write a bug report.
Instead I write to R-Help to ask you if this behavior can be considered a bug 
or not.
I started with a question at stackoverflow 
(https://stackoverflow.com/questions/74796994/r-traceback-performs-differently-in-interactive-mode-then-in-script-mode),
 but got no response there.

The strange behavior of base R lies in how it acts differently in an 
interactive R-Session then the execution of an R-Script.
As you see in the MWE below the traceback()-function returns NULL in script 
mode (first call) and an actual traceback in interactive mode (second call).

Can you reproduce this behavior?

Here is MWE, which contains a small test-script and the calls to it in an 
interactive and script mode:

user@server:~/folder> cat test.r

options(error = NULL)

onexit <- function() {
error.msg <- geterrmessage()
traceback <- traceback()

print(paste("error.msg: ", nchar(error.msg), sep = ""))
print(paste("traceback: ", is.null(traceback), " : ", traceback, sep = ""))

if (nchar(error.msg) != 0 && !is.null(traceback)) {
  print("Uncaught Error")
} else if (nchar(error.msg) != 0 && is.null(traceback)) {
 print("Caught Error")
} else if (nchar(error.msg) == 0 && is.null(traceback)) {
 print("No Error")
} else {
stop("ERROR in on.exit: bad traceback error.msg combination")
}
}

fail <- function() {
  on.exit(
  onexit(),
  add = TRUE,
  after = TRUE)

  print("SOMETHING")
  stop("BAD")
}

fail()

user@server:~/folder> docker run -it --rm -v /home/user/folder/:/data/R/ 
r-base:4.2.2 R -e 'source("/data/R/test.r")'

R version 4.2.2 (2022-10-31) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> source("/data/R/test.r")
[1] "SOMETHING"
Error in fail() : BAD
Calls: source -> withVisible -> eval -> eval -> fail
No traceback available
[1] "error.msg: 75"
[1] "traceback: TRUE : "
[1] "Caught Error"
Execution halted
user@server:~/folder> docker run -it --rm -v /home/user/folder/:/data/R/ 
r-base:4.2.2 R

R version 4.2.2 (2022-10-31) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> source("/data/R/test.r")
[1] "SOMETHING"
Error in fail() : BAD
6: stop("BAD") at test.r#30
5: fail() at test.r#33
4: eval(ei, envir)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("/data/R/test.r")
[1] "error.msg: 22"
[1] "traceback: FALSE : stop(\"BAD\")"
[2] "traceback: FALSE : fail()"
[3] "traceback: FALSE : eval(ei, envir)"
[4] "traceback: FALSE : eval(ei, envir)"
[5] "traceback: FALSE : withVisible(eval(ei, envir))"
[6] "traceback: FALSE : source(\"/data/R/test.r\")"
[1] "Uncaught Error"
>


Mit freundlichem Gruß / Best regards
WAGO GmbH & Co. KG

Malte Flender
Komplexitätsmanagement / Data Science
phone: +49 571 887-49779
fax: +49 571 887-849779
mailto: malte.flen...@wago.com

WAGO GmbH & Co.KG
Hansastraße 27
32423 Minden
Deutschland
http://www.wago.com<http://www.wago.com/>



Internal



 

 Diese E-Mail einschließlich ihrer Anhänge ist vertraulich und daher allein für 
den Gebrauch durch den vorgesehenen Empfänger bestimmt. Dritten ist das Lesen, 
Verteilen oder Weiterleiten dieser E-Mail sowie jedwedes Vertrauen auf deren 
Inhalt untersagt. Wi