Skip to content
Back to formatted view

Raw Message

Message-ID: <20240313110225.0c39202b@Tarkus>
Date: 2024-03-13T08:02:25Z
From: Ivan Krylov
Subject: Spurious warning in as.data.frame.factor()
In-Reply-To: <ace9e76c-bad0-4eb3-b3f8-c9cccd6e9a9d@gmail.com>

? Tue, 12 Mar 2024 12:33:17 -0700
Herv? Pag?s <hpages.on.github at 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