[Rd] Spurious warning in as.data.frame.factor()

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Thu Mar 14 10:41:54 CET 2024


> В Tue, 12 Mar 2024 12:33:17 -0700
> Hervé Pagès <hpages.on.github using gmail.com> пишет:

> > The acrobatics that as.data.frame.factor() is going thru in order to 
> > recognize a direct call don't play nice if as.data.frame() is an S4 
> > generic:

> >      df <- as.data.frame(factor(11:12))
> > 
> >      suppressPackageStartupMessages(library(BiocGenerics))
> >      isGeneric("as.data.frame")
> >      # [1] TRUE
> > 
> >      df <- as.data.frame(factor(11:12))
> >      # Warning message:
> >      # In as.data.frame.factor(factor(11:12)) :
> >      #   Direct call of 'as.data.frame.factor()' is deprecated.

aargh ... yes, all that's been a hack because in the end we do
want to get rid of 12  base (hence "exported") 
as.data.frame.<some>  methods, which will lead to *less*
acrobatics, but alas for now ...


> How about something like the following:

> Index: src/library/base/R/zzz.R
> ===================================================================
> --- src/library/base/R/zzz.R	(revision 86109)
> +++ src/library/base/R/zzz.R	(working copy)
> @@ -681,7 +681,14 @@
>      bdy <- body(as.data.frame.vector)
>      bdy <- bdy[c(1:2, seq_along(bdy)[-1L])] # taking [(1,2,2:n)] to insert at [2]:
>      ## deprecation warning only when not called by method dispatch from as.data.frame():
> -    bdy[[2L]] <- quote(if((sys.nframe() <= 1L || !identical(sys.function(-1L), as.data.frame)))
> +    bdy[[2L]] <- quote(if((sys.nframe() <= 1L || !(
> +	identical(sys.function(-1L), as.data.frame) || (
> +	    .isMethodsDispatchOn() &&
> +	    methods::is(sys.function(-1L), 'derivedDefaultMethod') &&
> +	    identical(
> +		sys.function(-1L)@generic,
> +		structure('as.data.frame', package = 'base')
> +	    )))))
>  	.Deprecated(
>  	    msg = gettextf(
>  		"Direct call of '%s()' is deprecated.  Use '%s()' or
>  		'%s()' instead",

Of course it's "sad" to need even more "acrobatics" ... but as it
should only be temporary {but several years:
       deprecation --> defunct --> complete }
I (as "responsible" of the gymnastics) am grateful for Ivan's proposed
additional S4-related conditions.

Anybody trying S7 examples and see if they work w/o producing
wrong warnings?


> The patch passes make check-devel, but I'm not sure how to safely put
> setGeneric('as.data.frame'); as.data.frame(factor(1:10)) in a
> regression test.

{What's the danger/problem?  we do have "similar" tests in both
  src/library/methods/tests/*.R
  tests/reg-S4.R

 -- maybe we can discuss bi-laterally  (or here, as you prefer)
}

Martin

> -- 
> Best regards,
> Ivan



More information about the R-devel mailing list