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

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Wed Mar 13 09:02:25 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.

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",

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.

-- 
Best regards,
Ivan



More information about the R-devel mailing list