Izmirlian, Grant (NIH/NCI) [E]
2024-Feb-06 21:14 UTC
[Rd] [EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments
Because functions get called and therefore, the calling sequence matters. It?s just protecting you from yourself, but as someone pointed out, there?s a way to silence such notes. G From: Herv? Pag?s <hpages.on.github at gmail.com> Sent: Tuesday, February 6, 2024 2:40 PM To: Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov>; Duncan Murdoch <murdoch.duncan at gmail.com>; r-devel at r-project.org Subject: Re: [EXTERNAL] Re: [Rd] NOTE: multiple local function definitions for ?fun? with different formal arguments On 2/6/24 11:19, Izmirlian, Grant (NIH/NCI) [E] wrote: The note refers to the fact that the function named ?fun? appears to be defined in two different ways. Sure I get that. But how is that any different from a variable being defined in two different ways like in if (mode == 1) x <- -8 else x <- 55 This is such a common and perfectly fine pattern. Why would this be considered a potential hazard when the variable is a function? H. From: Herv? Pag?s <hpages.on.github at gmail.com><mailto:hpages.on.github at gmail.com> Sent: Tuesday, February 6, 2024 2:17 PM To: Duncan Murdoch <murdoch.duncan at gmail.com><mailto:murdoch.duncan at gmail.com>; Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov><mailto:izmirlig at mail.nih.gov>; r-devel at r-project.org<mailto:r-devel at r-project.org> Subject: [EXTERNAL] Re: [Rd] NOTE: multiple local function definitions for ?fun? with different formal arguments Thanks. Workarounds are interesting but... what's the point of the NOTE in the first place? H. On 2/4/24 09:07, Duncan Murdoch wrote: On 04/02/2024 10:55 a.m., Izmirlian, Grant (NIH/NCI) [E] via R-devel wrote: Well you can see that yeast is exactly weekday you have. The way out is to just not name the result I think something happened to your explanation... toto <- function(mode) { ifelse(mode == 1, function(a,b) a*b, function(u, v, w) (u + v) / w) } It's a bad idea to use ifelse() when you really want if() ... else ... . In this case it works, but it doesn't always. So the workaround should be toto <- function(mode) { if(mode == 1) function(a,b) a*b else function(u, v, w) (u + v) / w } ________________________________ From: Grant Izmirlian <izmirlidroid at gmail.com><mailto:izmirlidroid at gmail.com> Date: Sun, Feb 4, 2024, 10:44 AM To: "Izmirlian, Grant (NIH/NCI) [E]" <izmirlig at mail.nih.gov><mailto:izmirlig at mail.nih.gov> Subject: Fwd: [EXTERNAL] R-devel Digest, Vol 252, Issue 2 Hi, I just ran into this 'R CMD check' NOTE for the first time: * checking R code for possible problems ... NOTE toto: multiple local function definitions for ?fun? with different formal arguments The "offending" code is something like this (simplified from the real code): toto <- function(mode) { if (mode == 1) fun <- function(a, b) a*b else fun <- function(u, v, w) (u + v) / w fun } Is that NOTE really intended? Hard to see why this code would be considered "wrong". I know it's just a NOTE but still... I agree it's a false positive, but the issue is that you have a function object in your function which can't be called unconditionally. The workaround doesn't create such an object. Recognizing that your function never tries to call fun requires global inspection of toto(), and most of the checks are based on local inspection. Duncan Murdoch ______________________________________________ R-devel at r-project.org<mailto:R-devel at r-project.org> mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Herv? Pag?s Bioconductor Core Team hpages.on.github at gmail.com<mailto:hpages.on.github at gmail.com> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and are confident the content is safe. -- Herv? Pag?s Bioconductor Core Team hpages.on.github at gmail.com<mailto:hpages.on.github at gmail.com> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and are confident the content is safe. [[alternative HTML version deleted]]
Henrik Bengtsson
2024-Feb-06 21:29 UTC
[Rd] [EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments
Here's a dummy example that I think illustrates the problem: toto <- function() { if (runif(1) < 0.5) function(a) a else function(a,b) a+b }> fcn <- toto() > fcn(1,2)[1] 3> fcn <- toto() > fcn(1,2)[1] 3> fcn <- toto() > fcn(1,2)Error in fcn(1, 2) : unused argument (2) How can you use the returned function, if you get different arguments? In your example, you cannot use the returned function without knowing 'mode', or by inspecting the returned function. So, the warning is there to alert you to a potential bug. Anecdotally, I'm pretty sure this R CMD check NOTE has caught at least one such bug in one of my/our packages. If you want to keep the current design pattern, one approach could be to add ... to your function definitions: toto <- function(mode) { if (mode == 1) fun <- function(a, b, ...) a*b else fun <- function(u, v, w) (u + v) / w fun } to make sure that toto() returns functions that accept the same minimal number of arguments. /Henrik On Tue, Feb 6, 2024 at 1:15?PM Izmirlian, Grant (NIH/NCI) [E] via R-devel <r-devel at r-project.org> wrote:> > Because functions get called and therefore, the calling sequence matters. It?s just protecting you from yourself, but as someone pointed out, there?s a way to silence such notes. > G > > > From: Herv? Pag?s <hpages.on.github at gmail.com> > Sent: Tuesday, February 6, 2024 2:40 PM > To: Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov>; Duncan Murdoch <murdoch.duncan at gmail.com>; r-devel at r-project.org > Subject: Re: [EXTERNAL] Re: [Rd] NOTE: multiple local function definitions for ?fun? with different formal arguments > > > On 2/6/24 11:19, Izmirlian, Grant (NIH/NCI) [E] wrote: > The note refers to the fact that the function named ?fun? appears to be defined in two different ways. > > Sure I get that. But how is that any different from a variable being defined in two different ways like in > > if (mode == 1) > x <- -8 > else > x <- 55 > > This is such a common and perfectly fine pattern. Why would this be considered a potential hazard when the variable is a function? > > H. > > From: Herv? Pag?s <hpages.on.github at gmail.com><mailto:hpages.on.github at gmail.com> > Sent: Tuesday, February 6, 2024 2:17 PM > To: Duncan Murdoch <murdoch.duncan at gmail.com><mailto:murdoch.duncan at gmail.com>; Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov><mailto:izmirlig at mail.nih.gov>; r-devel at r-project.org<mailto:r-devel at r-project.org> > Subject: [EXTERNAL] Re: [Rd] NOTE: multiple local function definitions for ?fun? with different formal arguments > > > Thanks. Workarounds are interesting but... what's the point of the NOTE in the first place? > > H. > On 2/4/24 09:07, Duncan Murdoch wrote: > On 04/02/2024 10:55 a.m., Izmirlian, Grant (NIH/NCI) [E] via R-devel wrote: > > > Well you can see that yeast is exactly weekday you have. The way out is to just not name the result > > I think something happened to your explanation... > > > > > toto <- function(mode) > { > ifelse(mode == 1, > function(a,b) a*b, > function(u, v, w) (u + v) / w) > } > > It's a bad idea to use ifelse() when you really want if() ... else ... . In this case it works, but it doesn't always. So the workaround should be > > > toto <- function(mode) > { > if(mode == 1) > function(a,b) a*b > else > function(u, v, w) (u + v) / w > } > > > > > > > ________________________________ > From: Grant Izmirlian <izmirlidroid at gmail.com><mailto:izmirlidroid at gmail.com> > Date: Sun, Feb 4, 2024, 10:44 AM > To: "Izmirlian, Grant (NIH/NCI) [E]" <izmirlig at mail.nih.gov><mailto:izmirlig at mail.nih.gov> > Subject: Fwd: [EXTERNAL] R-devel Digest, Vol 252, Issue 2 > > Hi, > > I just ran into this 'R CMD check' NOTE for the first time: > > * checking R code for possible problems ... NOTE > toto: multiple local function definitions for ?fun? with different > formal arguments > > The "offending" code is something like this (simplified from the real code): > > toto <- function(mode) > { > if (mode == 1) > fun <- function(a, b) a*b > else > fun <- function(u, v, w) (u + v) / w > fun > } > > Is that NOTE really intended? Hard to see why this code would be > considered "wrong". > > I know it's just a NOTE but still... > > I agree it's a false positive, but the issue is that you have a function object in your function which can't be called unconditionally. The workaround doesn't create such an object. > > Recognizing that your function never tries to call fun requires global inspection of toto(), and most of the checks are based on local inspection. > > Duncan Murdoch > > ______________________________________________ > R-devel at r-project.org<mailto:R-devel at r-project.org> mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > -- > > Herv? Pag?s > > > > Bioconductor Core Team > > hpages.on.github at gmail.com<mailto:hpages.on.github at gmail.com> > CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and are confident the content is safe. > > > -- > > Herv? Pag?s > > > > Bioconductor Core Team > > hpages.on.github at gmail.com<mailto:hpages.on.github at gmail.com> > CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and are confident the content is safe. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Possibly Parallel Threads
- [EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments
- NOTE: multiple local function definitions for ?fun? with different formal arguments
- NOTE: multiple local function definitions for ?fun? with different formal arguments
- NOTE: multiple local function definitions for ?fun? with different formal arguments
- [EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments