Skip to content

Compatibility issues caused by new simplify argument in apply function

6 messages · Lukas Lehnert, Duncan Murdoch, Henrik Bengtsson +1 more

#
Dear R Developers,

the new  simplify argument in apply causes that my package (hsdar) does not 
pass the 
checks in R-devel.

The workaround, Kurt Hornik send me, is working for the R-code:
if("simplify" %in% names(formals(base::apply))) 
 do something 
else 
 do something else

Unfortunately, I cannot conditionalize the man pages of the functions. I get 
the message 
that "applySpeclib.Rd:12-14: Section \Sexpr is unrecognized and will be 
dropped" if I try to 
dynamically define the entire usage section. If I try to use \Sexpr inside the 
\usage section, 
I get the following warning: "applySpeclib.Rd:13-15: Tag \Sexpr is invalid in 
a \usage block"

Does anybody have an idea how to proceed. The full code is available below.

Thanks

Lukas


*1. Code for full usage section:*
..
\description{
Apply function over all spectra or a subset of spectra in a \code{Speclib}.
}

\Sexpr[echo=TRUE,results=rd,stage=install]{
  hsdar:::.applyInHelp1("Speclib", usage = TRUE)
}

\arguments{
..

*Function .applyInHelp1*
.applyInHelp1 <- function(fun_name, usage)
{
  if (usage)
  {
    if ("simplify" %in% names(formals(base::apply))) 
    {
      return(paste0("\\usage{\n",
                    "\\S4method{apply}{", fun_name, "}(X, MARGIN, FUN, ..., 
simplify = TRUE)\n",
                    "}"))
    } else {
      return(paste0("\\usage{\n",
                    "\\S4method{apply}{", fun_name, "}(X, MARGIN, FUN, ...)
\n",
                    "}"))
    }
  } else {
    if ("simplify" %in% names(formals(base::apply))) 
    {
      return("}\n\\item{simplify}{Currently ignored")
    } else {
      return("")
    }
  }
}


*2. Using \Sexpr inside the \usage block*
\usage{
\S4method{apply}{Speclib}(X, FUN, bySI = NULL, ...
\Sexpr[echo=TRUE,results=rd,stage=install]{
  hsdar:::.applyInHelp2(usage = TRUE)
}
)
}


*Function .applyInHelp2*
.applyInHelp2 <- function(usage)
{
  if (usage)
  {
    if ("simplify" %in% names(formals(base::apply))) 
    {
      return(", simplify = TRUE)")
    } 
  } else {
    if ("simplify" %in% names(formals(base::apply))) 
    {
      return("}\n\\item{simplify}{Currently ignored")
    } else {
      return("")
    }
  }
}
#
You didn't explained what the error is.  This is what it looks like to 
me, but I'm probably wrong in some details:

1. R-devel added an argument to the apply() function, so the header has 
changed from

   function (X, MARGIN, FUN, ...)

to

   function(X, MARGIN, FUN, ..., simplify = TRUE)

2. Your package converted the function apply() to an S4 generic.

3. Now the signatures of your methods for this generic need to have the 
simplify argument, but if you do that, they won't work in previous 
versions of R.

You'd like to have conditional code and documentation to depend on the 
version of R.

Is that all correct?

I don't think it's possible, for the reasons you found.  Certainly you 
can have conditional code, but the docs are going to fail.

One thing that might work is in versions of R before this change, export 
your own version of apply, with the change in place, i.e.

if(!("simplify" %in% names(formals(base::apply))))
   apply <- function(X, MARGIN, FUN, ..., simplify = TRUE) {
     base::apply(X, MARGIN, FUN, ...)
   }

and then conditionally export "apply" in these old versions.  Then your 
docs could match the new version everywhere.

Another thing is to maintain two versions of your package, one for R 
versions before the change, another for versions after the change.  Add 
appropriate entries in the DESCRIPTION file, e.g.

Depends:  R (> 4.0)

Another is to argue with R Core that this change to a really old 
function is too hard to accommodate, and they should back it out, maybe 
by making a new function with the new signature.

Or you could make a new function with the old signature, and use that 
instead of apply().

Duncan Murdoch
On 22/05/2020 6:26 a.m., Lukas Lehnert via R-devel wrote:
#
Interesting problem.  I'm very rusty on S4 but would one solution be
to, already now, add 'simplify = TRUE' to the S4 method and document
it;

setMethod("apply", signature(X = "Speclib"),
          function(X,
                   FUN,
                   bySI = NULL,
                   ...,
                   simplify = TRUE) {

?

Henrik
On Fri, May 22, 2020 at 6:26 AM Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
#
On 22/05/2020 11:47 a.m., Henrik Bengtsson wrote:
Yes, that sounds like an ideal solution.  The docs can say it is ignored 
in old versions and passed on in new ones.

Duncan Murdoch
#
I am sorry for being not specific enough. Both of you were right with your 
guess how the initial problem looked like.

I followed the suggestion of Henrik and at least on my computer it seems to 
work (sometimes solutions are much easier than you think). Let's see what CRAN 
tells me...

Thank you for your time and thinking about the problem!

Best

Lukas

Am Freitag, 22. Mai 2020, 17:47:00 CEST schrieb Henrik Bengtsson:
wrote:
#
> Dear R Developers,
    > the new  simplify argument in apply causes that my package (hsdar) does not 
    > pass the 
    > checks in R-devel.

    > The workaround, Kurt Hornik send me, is working for the R-code:
    > if("simplify" %in% names(formals(base::apply))) 
    > do something 
    > else 
    > do something else

    > Unfortunately, I cannot conditionalize the man pages of the functions. 

Why should you do that?   In other words:  Why change the
argument list of your function(s) ?

That \Sexpr{} does not work as a section is "obvious" (for some),
but I really don't see why you should change the argument list
or defaults of your hdara package functions at all.

Martin Maechler
ETH Zurich  and  R Core

    > I get the message 
    > that "applySpeclib.Rd:12-14: Section \Sexpr is unrecognized and will be 
    > dropped" if I try to 
    > dynamically define the entire usage section. If I try to use \Sexpr inside the 
    > \usage section, 
    > I get the following warning: "applySpeclib.Rd:13-15: Tag \Sexpr is invalid in 
    > a \usage block"

    > Does anybody have an idea how to proceed. The full code is available below.

    > Thanks

    > Lukas


    > *1. Code for full usage section:*
    > ..
    > \description{
    > Apply function over all spectra or a subset of spectra in a \code{Speclib}.
    > }

    > \Sexpr[echo=TRUE,results=rd,stage=install]{
    > hsdar:::.applyInHelp1("Speclib", usage = TRUE)
    > }

    > \arguments{
    > ..

    > *Function .applyInHelp1*
    > .applyInHelp1 <- function(fun_name, usage)
    > {
    > if (usage)
    > {
    > if ("simplify" %in% names(formals(base::apply))) 
    > {
    > return(paste0("\\usage{\n",
    > "\\S4method{apply}{", fun_name, "}(X, MARGIN, FUN, ..., 
    > simplify = TRUE)\n",
    > "}"))
    > } else {
    > return(paste0("\\usage{\n",
    > "\\S4method{apply}{", fun_name, "}(X, MARGIN, FUN, ...)
    > \n",
    > "}"))
    > }
    > } else {
    > if ("simplify" %in% names(formals(base::apply))) 
    > {
    > return("}\n\\item{simplify}{Currently ignored")
    > } else {
    > return("")
    > }
    > }
    > }


    > *2. Using \Sexpr inside the \usage block*
    > \usage{
    > \S4method{apply}{Speclib}(X, FUN, bySI = NULL, ...
    > \Sexpr[echo=TRUE,results=rd,stage=install]{
    > hsdar:::.applyInHelp2(usage = TRUE)
    > }
    > )
    > }


    > *Function .applyInHelp2*
    > .applyInHelp2 <- function(usage)
    > {
    > if (usage)
    > {
    > if ("simplify" %in% names(formals(base::apply))) 
    > {
    > return(", simplify = TRUE)")
    > } 
    > } else {
    > if ("simplify" %in% names(formals(base::apply))) 
    > {
    > return("}\n\\item{simplify}{Currently ignored")
    > } else {
    > return("")
    > }
    > }
    > }

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel