[R] Error in summary.warnings?
Allison Meisner
@me|@ne1 @end|ng |rom jhm|@edu
Sat May 9 18:39:07 CEST 2020
Great! Thanks for sharing your fix.
Allison
________________________________
From: Martin Maechler <maechler using stat.math.ethz.ch>
Sent: Saturday, May 9, 2020 11:37 AM
To: Allison Meisner <ameisne1 using jhmi.edu>; r-help using r-project.org <r-help using r-project.org>
Cc: Martin Maechler <maechler using stat.math.ethz.ch>
Subject: Re: [R] Error in summary.warnings?
>>>>> Martin Maechler
>>>>> on Fri, 8 May 2020 17:37:29 +0200 writes:
>>>>> Allison Meisner
>>>>> on Thu, 7 May 2020 19:32:36 +0000 writes:
> I believe there is an error in the summary.warnings function (typically called via 'summary(warnings())'). Below is a minimal working example:
> #########
> testfunction <- function(x){
> if(x > 30){
> warning("A big problem (should be 20 of these)")
> }else{
> warning("Bigger problem (should be 30 of these)")
> }
> }
> for(i in 1:50){
> testfunction(i)
> }
> summary(warnings())
> #########
> I checked the code for summary.warnings:
> function (object, ...)
> {
> msgs <- names(object)
> calls <- as.character(object)
> ss <- ": "
> c.m. <- paste(calls, msgs, sep = ss)
> if(length(i.no.call <- which(calls == "NULL")))
> c.m.[i.no.call] <- substr(c.m.[i.no.call],
> nchar(paste0("NULL", ss))+1L, 100000L)
> tm <- table(c.m., deparse.level = 0L)
> structure(unique(object), counts = as.vector(tm), class = "summary.warnings")
> }
>> The problem appears to be in the last line: unique preserves the order of the input, but counts reflects the counts in the table tm, which is a problem because table names are in alphabetical order.
I've committed the fix. If you are interested,
I've replaced the last 2 lines with
i.uniq <- which(!duplicated(object, incomparables=FALSE))
tm <- table(factor(c.m., levels=c.m.[i.uniq]), deparse.level=0L)
structure(object[i.uniq], counts = as.vector(tm), class = "summary.warnings")
which (at least conceptually) should even be faster the previous code.
Thank you again,
Martin
>> Am I missing something?
> No -- I think you are perfect and I was very imperfect ;-) when
> I created and tested the function ..
> This will be fixed in the next versions of R.
> Thank you very much for the report and the nice concise
> reproducible example!
> Best regards,
> Martin
>> Allison
>> ----------
>> Allison Meisner, PhD
>> Postdoctoral Fellow
>> Department of Biostatistics
>> Johns Hopkins Bloomberg School of Public Health
>> 615 N. Wolfe Street
>> Baltimore, MD 21205
> Martin Maechler
> ETH Zurich and R Core team
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help
mailing list