Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

2020-01-18 Thread Joris Meys
Hi Brian,

I agree that the way R CMD check reports on this, can be improved. 
For your case (as far as I understand it), you can try dispatching explicitly 
on the first element of the dots by using the object argument of UseMethod. 
This works for me.

foo <- function(..., data){
  UseMethod("foo", ..1)
}

foo.factor <- function(..., data){
  x <- list(...)
  x$data <- data
  x
} 
foo.formula <- function(..., data){
  x <- list(...)
  x$data <- data
  x
}

Cheers
Joris

--
Joris Meys
Statistical consultant

Department of Data Analysis and Mathematical Modelling
Ghent University
Coupure Links 653, B-9000 Gent (Belgium)
--

Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



From: Smith, Brian J 
Sent: Friday, January 17, 2020 9:18 PM
To: Smith, Brian J; Joris Meys; Duncan Murdoch; r-package-devel@r-project.org
Subject: RE: [R-pkg-devel]  [External] Re: S3 generic/method consistency 
warning for formula method

I spoke too soon.  dotsMethods will not work for me since it does not allow for 
mixing of the ellipsis with other formal arguments.  For example, the following 
will not work if argument 'data' is not a formula; e.g. a data frame.

setGeneric("foo2", function(...) standardGeneric("foo2"))

setMethod("foo2", "formula",
  function(..., data) list(...)
)

What is the recommended course of action if the validity of an R CMD check 
warning is in doubt?  In this case, the warning message indicates an 
inconsistency that does not exist and references documentation that does not 
seem to explain the actual issue.

-Original Message-
From: R-package-devel  On Behalf Of 
Smith, Brian J
Sent: Friday, January 17, 2020 11:45 AM
To: Joris Meys ; Duncan Murdoch 
; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning 
for formula method

Thanks for the tip about dotsMethods.  I am mainly working with S4 objects in 
my package, so that approach would fit well.  The following S4 method seems to 
work and pass R CMD checks.

setGeneric("foo2", function(...) standardGeneric("foo2"))

setMethod("foo2", "formula",
  function(...) list(...)
)

In my actual implementation of S3 methods, I do check that all ... elements are 
of the same class to avoid ending up in a world of hurt.  Converting them to S4 
methods will save me from having to do those checks - nice!

>From a language definition point of view though, I am still bothered that a 
>check warning is triggered by the S3 formula method but not by others.  That 
>just seems inconsistent, and I have not yet seen any language documentation to 
>explain the inconsistency.  Are we sure this is not an issue with the R CMD 
>check implementation?

Nevertheless, it looks like I have a way to get rid of the warning.

-Original Message-
From: Joris Meys 
Sent: Friday, January 17, 2020 10:12 AM
To: Smith, Brian J ; Duncan Murdoch 
; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning 
for formula method

Hi Brian,

I get what you want to do, but S3 isn't suited for that at all. If all elements 
in ... have to be the same class, you can use S4 (see ?dotsMethods). If not, 
then using S3 makes even less sense. Take following example:

x <- list()
y <- c(1,2)

foo(x,y) will dispatch to foo.list()
foo(y,x) will dispatch to foo.character()

You'll be in a world of hurt if you want to maintain or update that code. If 
you insist on using S3, you'll have to fix the position (and hence the name) of 
the argument you want to dispatch on.

my 2 cents
Cheers
Joris

--
Joris Meys
Statistical consultant

Department of Data Analysis and Mathematical Modelling Ghent University Coupure 
Links 653, B-9000 Gent (Belgium)
--

Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



From: R-package-devel  on behalf of 
Smith, Brian J 
Sent: Friday, January 17, 2020 4:59 PM
To: Duncan Murdoch; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel]  [External] Re: S3 generic/method consistency 
warning for formula method

Thanks Duncan.

I was surprised too when first realizing this was possible.  I believe the 
reason it works is that UseMethod dispatches on the first supplied argument by 
default.  So, really the dispatch is on the first element of the ellipsis.  The 
'c' function works like this, albeit as a primitive function.

I would like to dispatch on the ellipsis to allow a user to name values being 
passed in.  Defining an initial x argument would indeed eliminate the warning, 
but would not allow for user-naming of the value.  I could have the user pass a 
list of values to x and do dispatching manually, but that seems like more steps 
for the user and more complication for the i

Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

2020-01-17 Thread Smith, Brian J
I spoke too soon.  dotsMethods will not work for me since it does not allow for 
mixing of the ellipsis with other formal arguments.  For example, the following 
will not work if argument 'data' is not a formula; e.g. a data frame.

setGeneric("foo2", function(...) standardGeneric("foo2"))

setMethod("foo2", "formula",
  function(..., data) list(...)
)

What is the recommended course of action if the validity of an R CMD check 
warning is in doubt?  In this case, the warning message indicates an 
inconsistency that does not exist and references documentation that does not 
seem to explain the actual issue.

-Original Message-
From: R-package-devel  On Behalf Of 
Smith, Brian J
Sent: Friday, January 17, 2020 11:45 AM
To: Joris Meys ; Duncan Murdoch 
; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning 
for formula method

Thanks for the tip about dotsMethods.  I am mainly working with S4 objects in 
my package, so that approach would fit well.  The following S4 method seems to 
work and pass R CMD checks.

setGeneric("foo2", function(...) standardGeneric("foo2"))

setMethod("foo2", "formula",
  function(...) list(...)
)

In my actual implementation of S3 methods, I do check that all ... elements are 
of the same class to avoid ending up in a world of hurt.  Converting them to S4 
methods will save me from having to do those checks - nice!

>From a language definition point of view though, I am still bothered that a 
>check warning is triggered by the S3 formula method but not by others.  That 
>just seems inconsistent, and I have not yet seen any language documentation to 
>explain the inconsistency.  Are we sure this is not an issue with the R CMD 
>check implementation?

Nevertheless, it looks like I have a way to get rid of the warning.

-Original Message-
From: Joris Meys 
Sent: Friday, January 17, 2020 10:12 AM
To: Smith, Brian J ; Duncan Murdoch 
; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning 
for formula method

Hi Brian,

I get what you want to do, but S3 isn't suited for that at all. If all elements 
in ... have to be the same class, you can use S4 (see ?dotsMethods). If not, 
then using S3 makes even less sense. Take following example:

x <- list()
y <- c(1,2)

foo(x,y) will dispatch to foo.list()
foo(y,x) will dispatch to foo.character()

You'll be in a world of hurt if you want to maintain or update that code. If 
you insist on using S3, you'll have to fix the position (and hence the name) of 
the argument you want to dispatch on.

my 2 cents
Cheers
Joris

--
Joris Meys
Statistical consultant

Department of Data Analysis and Mathematical Modelling Ghent University Coupure 
Links 653, B-9000 Gent (Belgium)
--

Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



From: R-package-devel  on behalf of 
Smith, Brian J 
Sent: Friday, January 17, 2020 4:59 PM
To: Duncan Murdoch; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel]  [External] Re: S3 generic/method consistency 
warning for formula method

Thanks Duncan.

I was surprised too when first realizing this was possible.  I believe the 
reason it works is that UseMethod dispatches on the first supplied argument by 
default.  So, really the dispatch is on the first element of the ellipsis.  The 
'c' function works like this, albeit as a primitive function.

I would like to dispatch on the ellipsis to allow a user to name values being 
passed in.  Defining an initial x argument would indeed eliminate the warning, 
but would not allow for user-naming of the value.  I could have the user pass a 
list of values to x and do dispatching manually, but that seems like more steps 
for the user and more complication for the implementation than ought to be 
necessary.  There are other downsides to that approach in my particularly 
application...

The issue mentioned below about calling the function with no arguments can be 
solved by defining the following default method.

foo.default <- function(...) list(...)

__
R-package-devel@r-project.org mailing list 
https://stat.ethz.ch/mailman/listinfo/r-package-devel

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

2020-01-17 Thread Smith, Brian J
That's an interesting solution too.

-Original Message-
From: Duncan Murdoch  
Sent: Friday, January 17, 2020 11:17 AM
To: Smith, Brian J ; r-package-devel@r-project.org
Subject: Re: [External] Re: [R-pkg-devel] S3 generic/method consistency warning 
for formula method

On 17/01/2020 10:59 a.m., Smith, Brian J wrote:
> Thanks Duncan.
> 
> I was surprised too when first realizing this was possible.  I believe the 
> reason it works is that UseMethod dispatches on the first supplied argument 
> by default.  So, really the dispatch is on the first element of the ellipsis. 
>  The 'c' function works like this, albeit as a primitive function.
> 
> I would like to dispatch on the ellipsis to allow a user to name values being 
> passed in.  Defining an initial x argument would indeed eliminate the 
> warning, but would not allow for user-naming of the value.  I could have the 
> user pass a list of values to x and do dispatching manually, but that seems 
> like more steps for the user and more complication for the implementation 
> than ought to be necessary.  There are other downsides to that approach in my 
> particularly application...

I think you can still do that, if your generic looks like this:

foo <- function(x, ...) {
   if (missing(x))
 UseMethod("foo", ..1)
   else
 UseMethod("foo")
}

and your methods look like this one:

foo.numeric <- function(x, ...) if (missing(x)) list(...) else list(x, ...)

There are some negatives to this solution:

  - it's more complicated than what you have
  - foo() now doesn't work unless you make it even more complicated
  - the name x is now special; foo("a", 1) will now dispatch to a different 
method than foo(w = "a", x = 1).  This is getting into the "world of hurt" that 
Joris mentioned.

The benefits are non-trivial:

  - It should make that warning go away
  - It doesn't depend on undocumented behaviour.

Duncan Murdoch

> 
> The issue mentioned below about calling the function with no arguments can be 
> solved by defining the following default method.
> 
> foo.default <- function(...) list(...)
> 
> -Original Message-
> From: Duncan Murdoch 
> Sent: Friday, January 17, 2020 9:24 AM
> To: Smith, Brian J ; 
> r-package-devel@r-project.org
> Subject: [External] Re: [R-pkg-devel] S3 generic/method consistency 
> warning for formula method
> 
> On 17/01/2020 9:14 a.m., Smith, Brian J wrote:
>> Hello all,
>>
>> I am getting an R CMD check warning about S3 consistency for a formula 
>> method of a generic function that dispatches on the ellipsis argument.  The 
>> formula method seems to function properly when called, and methods for other 
>> types tried do not trigger the warning.  Below is an example generic 
>> function 'foo' and methods that will reproduce the issue, followed by the 
>> check warning message produced.
>>
>> foo <- function(...) UseMethod("foo") foo.factor <- function(...) 
>> list(...) foo.formula <- function(...) list(...) foo.logical <- 
>> function(...)
>> list(...) foo.matrix <- function(...) list(...) foo.numeric <-
>> function(...) list(...)
> 
> I'm surprised that you are allowed to have a generic with no arguments 
> except "...".  I imagine the error will go away if you change the 
> definitions to
> 
> foo <- function(x, ...) UseMethod("foo") foo.factor <- function(x, 
> ...) list(x, ...) foo.formula <- function(x, ...) list(x, ...) 
> foo.logical <- function(x, ...) list(x, ...) foo.matrix <- function(x, 
> ...) list(x, ...) foo.numeric <- function(x, ...) list(x, ...)
> 
> With your original definition, it would be legal to call foo(), but 
> the
> UseMethod() would fail.
> 
> Duncan Murdoch
> 
>>
>> R CMD check warning:
>>
>> W  checking S3 generic/method consistency (1.7s)
>>  foo:
>>function(...)
>>  foo.formula:
>>function(...)
>>  
>>  See section 'Generic functions and methods' in the 'Writing R
>>  Extensions' manual.
>>
>> I would appreciate any help in understanding and addressing this check 
>> warning.
>>
>> Thank you,
>> Brian
>>
>> *
>> Brian J Smith, PhD
>> Professor, Department of Biostatistics Director, Biostatistics Core, 
>> Holden Comprehensive Cancer Center The University of Iowa
>> 145 North Riverside Drive, N311 CPHB
>> Phone: 319-384-1587
>> Email: brian-j-sm...@uiowa.edu
>>
>> __
>> R-package-devel@r-project.org mailing list 
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>>
> 

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

2020-01-17 Thread Smith, Brian J
Thanks for the tip about dotsMethods.  I am mainly working with S4 objects in 
my package, so that approach would fit well.  The following S4 method seems to 
work and pass R CMD checks.

setGeneric("foo2", function(...) standardGeneric("foo2"))

setMethod("foo2", "formula",
  function(...) list(...)
)

In my actual implementation of S3 methods, I do check that all ... elements are 
of the same class to avoid ending up in a world of hurt.  Converting them to S4 
methods will save me from having to do those checks - nice!

>From a language definition point of view though, I am still bothered that a 
>check warning is triggered by the S3 formula method but not by others.  That 
>just seems inconsistent, and I have not yet seen any language documentation to 
>explain the inconsistency.  Are we sure this is not an issue with the R CMD 
>check implementation?

Nevertheless, it looks like I have a way to get rid of the warning.

-Original Message-
From: Joris Meys  
Sent: Friday, January 17, 2020 10:12 AM
To: Smith, Brian J ; Duncan Murdoch 
; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning 
for formula method

Hi Brian,

I get what you want to do, but S3 isn't suited for that at all. If all elements 
in ... have to be the same class, you can use S4 (see ?dotsMethods). If not, 
then using S3 makes even less sense. Take following example:

x <- list()
y <- c(1,2)

foo(x,y) will dispatch to foo.list()
foo(y,x) will dispatch to foo.character()

You'll be in a world of hurt if you want to maintain or update that code. If 
you insist on using S3, you'll have to fix the position (and hence the name) of 
the argument you want to dispatch on.

my 2 cents
Cheers
Joris

--
Joris Meys
Statistical consultant

Department of Data Analysis and Mathematical Modelling Ghent University Coupure 
Links 653, B-9000 Gent (Belgium)
--

Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



From: R-package-devel  on behalf of 
Smith, Brian J 
Sent: Friday, January 17, 2020 4:59 PM
To: Duncan Murdoch; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel]  [External] Re: S3 generic/method consistency 
warning for formula method

Thanks Duncan.

I was surprised too when first realizing this was possible.  I believe the 
reason it works is that UseMethod dispatches on the first supplied argument by 
default.  So, really the dispatch is on the first element of the ellipsis.  The 
'c' function works like this, albeit as a primitive function.

I would like to dispatch on the ellipsis to allow a user to name values being 
passed in.  Defining an initial x argument would indeed eliminate the warning, 
but would not allow for user-naming of the value.  I could have the user pass a 
list of values to x and do dispatching manually, but that seems like more steps 
for the user and more complication for the implementation than ought to be 
necessary.  There are other downsides to that approach in my particularly 
application...

The issue mentioned below about calling the function with no arguments can be 
solved by defining the following default method.

foo.default <- function(...) list(...)

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

2020-01-17 Thread Joris Meys
umfff. 

foo(y,x) will dispatch to foo.numeric() obviously


__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

2020-01-17 Thread Joris Meys
Hi Brian,

I get what you want to do, but S3 isn't suited for that at all. If all elements 
in ... have to be the same class, you can use S4 (see ?dotsMethods). If not, 
then using S3 makes even less sense. Take following example:

x <- list()
y <- c(1,2)

foo(x,y) will dispatch to foo.list()
foo(y,x) will dispatch to foo.character()

You'll be in a world of hurt if you want to maintain or update that code. If 
you insist on using S3, you'll have to fix the position (and hence the name) of 
the argument you want to dispatch on.

my 2 cents
Cheers
Joris

--
Joris Meys
Statistical consultant

Department of Data Analysis and Mathematical Modelling
Ghent University
Coupure Links 653, B-9000 Gent (Belgium)
--

Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



From: R-package-devel  on behalf of 
Smith, Brian J 
Sent: Friday, January 17, 2020 4:59 PM
To: Duncan Murdoch; r-package-devel@r-project.org
Subject: Re: [R-pkg-devel]  [External] Re: S3 generic/method consistency 
warning for formula method

Thanks Duncan.

I was surprised too when first realizing this was possible.  I believe the 
reason it works is that UseMethod dispatches on the first supplied argument by 
default.  So, really the dispatch is on the first element of the ellipsis.  The 
'c' function works like this, albeit as a primitive function.

I would like to dispatch on the ellipsis to allow a user to name values being 
passed in.  Defining an initial x argument would indeed eliminate the warning, 
but would not allow for user-naming of the value.  I could have the user pass a 
list of values to x and do dispatching manually, but that seems like more steps 
for the user and more complication for the implementation than ought to be 
necessary.  There are other downsides to that approach in my particularly 
application...

The issue mentioned below about calling the function with no arguments can be 
solved by defining the following default method.

foo.default <- function(...) list(...)


__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

2020-01-17 Thread Smith, Brian J
Thanks Duncan.

I was surprised too when first realizing this was possible.  I believe the 
reason it works is that UseMethod dispatches on the first supplied argument by 
default.  So, really the dispatch is on the first element of the ellipsis.  The 
'c' function works like this, albeit as a primitive function.

I would like to dispatch on the ellipsis to allow a user to name values being 
passed in.  Defining an initial x argument would indeed eliminate the warning, 
but would not allow for user-naming of the value.  I could have the user pass a 
list of values to x and do dispatching manually, but that seems like more steps 
for the user and more complication for the implementation than ought to be 
necessary.  There are other downsides to that approach in my particularly 
application...

The issue mentioned below about calling the function with no arguments can be 
solved by defining the following default method.

foo.default <- function(...) list(...)

-Original Message-
From: Duncan Murdoch  
Sent: Friday, January 17, 2020 9:24 AM
To: Smith, Brian J ; r-package-devel@r-project.org
Subject: [External] Re: [R-pkg-devel] S3 generic/method consistency warning for 
formula method

On 17/01/2020 9:14 a.m., Smith, Brian J wrote:
> Hello all,
> 
> I am getting an R CMD check warning about S3 consistency for a formula method 
> of a generic function that dispatches on the ellipsis argument.  The formula 
> method seems to function properly when called, and methods for other types 
> tried do not trigger the warning.  Below is an example generic function 'foo' 
> and methods that will reproduce the issue, followed by the check warning 
> message produced.
> 
> foo <- function(...) UseMethod("foo")
> foo.factor <- function(...) list(...)
> foo.formula <- function(...) list(...) foo.logical <- function(...) 
> list(...) foo.matrix <- function(...) list(...) foo.numeric <- 
> function(...) list(...)

I'm surprised that you are allowed to have a generic with no arguments except 
"...".  I imagine the error will go away if you change the definitions to

foo <- function(x, ...) UseMethod("foo") foo.factor <- function(x, ...) list(x, 
...) foo.formula <- function(x, ...) list(x, ...) foo.logical <- function(x, 
...) list(x, ...) foo.matrix <- function(x, ...) list(x, ...) foo.numeric <- 
function(x, ...) list(x, ...)

With your original definition, it would be legal to call foo(), but the
UseMethod() would fail.

Duncan Murdoch

> 
> R CMD check warning:
> 
> W  checking S3 generic/method consistency (1.7s)
> foo:
>   function(...)
> foo.formula:
>   function(...)
> 
> See section 'Generic functions and methods' in the 'Writing R
> Extensions' manual.
> 
> I would appreciate any help in understanding and addressing this check 
> warning.
> 
> Thank you,
> Brian
> 
> *
> Brian J Smith, PhD
> Professor, Department of Biostatistics Director, Biostatistics Core, 
> Holden Comprehensive Cancer Center The University of Iowa
> 145 North Riverside Drive, N311 CPHB
> Phone: 319-384-1587
> Email: brian-j-sm...@uiowa.edu
> 
> __
> R-package-devel@r-project.org mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
> 

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel