Skip to content

[R-pkg-devel] Forward function call

7 messages · Göran Broström, Neal Fultz, William Dunlap +3 more

#
Hello,

the function 'coxreg' in my package 'eha' is often just a wrapper for 
'coxph' in survival, so I have code like

     if (cox.ph){
         Call <- match.call()
         Call[[1]] <- as.name("coxph")
         fit <- eval.parent(Call)
         return(fit)
     }

which works since eha depends on survival. Now I am thinking of changing 
Depends to Imports, and I would like to change the code to

     if (cox.ph){
         Call <- match.call()
         Call[[1]] <- as.name("survival::coxph")
         fit <- eval.parent(Call)
         return(fit)
     }

but this doesn't work, because the function name is turned into
`survival::coxph` (with the backticks) and the evaluation fails.

How can I solve this?

Thanks, G,
#
You will first need to convert the function call to an expression, then you
can modify the expression as appropriate.

Here's a somewhat contrived minimal example:
language f(z = 2, a = 1, 3)
expression(f(z = 2, a = 1, 3))
expression(base::c(z = 2, a = 1, 3))
z a
2 1 3



On Mon, Jun 8, 2020 at 12:12 PM G?ran Brostr?m <goran.brostrom at umu.se>
wrote:

  
  
#
Use
   Call[[1]] <- quote(survival::coxph)

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Mon, Jun 8, 2020 at 12:12 PM G?ran Brostr?m <goran.brostrom at umu.se>
wrote:

  
  
#
I think quote(survival::coxph) will work in place of as.name() ?
On Mon, Jun 8, 2020 at 3:12 PM G?ran Brostr?m <goran.brostrom at umu.se> wrote:
#
Thanks for the responses!

I found the suggestion

Call[[1]] <- quote(survival::coxph)

easiest to implement. And it works.

Best, G?ran
On 2020-06-08 21:42, Ben Bolker wrote:
#
> Thanks for the responses!
    > I found the suggestion

    > Call[[1]] <- quote(survival::coxph)

    > easiest to implement. And it works.

and it is what we use in R's own R source code in several
places (and that's where/how I assume it also came to  Ben
Bolker, lme4, etc) :

A simple grep inside current R's source <R>/src/library/ gives

grep -r --color -nHF -e 'quote(stats::'

./stats/R/acf.R:28:        m[[1L]] <- quote(stats::pacf)
./stats/R/aggregate.R:154:    m[[1L]] <- quote(stats::model.frame)
./stats/R/aov.R:36:    lmcall[[1L]] <- quote(stats::lm)
./stats/R/bartlett.test.R:86:    m[[1L]] <- quote(stats::model.frame)
./stats/R/cor.test.R:213:    m[[1L]] <- quote(stats::model.frame)
./stats/R/factanal.R:73:            mf[[1L]] <- quote(stats::model.frame)
./stats/R/friedman.test.R:92:    m[[1L]] <- quote(stats::model.frame)
./stats/R/ftable.R:150:        m[[1L]] <- quote(stats::model.frame)
./stats/R/glm.R:52:    mf[[1L]] <- quote(stats::model.frame)
./stats/R/glm.R:863:	fcall[[1L]] <- quote(stats::glm)
./stats/R/lm.R:34:    mf[[1L]] <- quote(stats::model.frame)
./stats/R/lm.R:546:        fcall[[1L]] <- quote(stats::model.frame)
./stats/R/loess.R:34:    mf[[1L]] <- quote(stats::model.frame)
./stats/R/manova.R:22:    fcall[[1L]] <- quote(stats::aov)
./stats/R/model.tables.R:485:    fcall <- c(list(quote(stats::model.frame)), args)
./stats/R/nls.R:570:            mf[[1L]] <- quote(stats::model.frame)
./stats/R/ppr.R:30:    m[[1L]] <- quote(stats::model.frame)
./stats/R/prcomp.R:69:    mf[[1L]] <- quote(stats::model.frame)
./stats/R/princomp.R:30:    mf[[1L]] <- quote(stats::model.frame)
./stats/R/quade.test.R:102:    m[[1L]] <- quote(stats::model.frame)
./stats/R/spectrum.R:220:        m[[1L]] <- quote(stats::plot.spec.coherency)
./stats/R/spectrum.R:226:        m[[1L]] <- quote(stats::plot.spec.phase)
./stats/R/t.test.R:141:    m[[1L]] <- quote(stats::model.frame)
./stats/R/ts.R:744:    m[[1L]] <- quote(stats::window)
./stats/R/var.test.R:97:    m[[1L]] <- quote(stats::model.frame)
./stats/R/xtabs.R:40:    m[[1L]] <- quote(stats::model.frame)


    > Best, G?ran
> On 2020-06-08 21:42, Ben Bolker wrote:
>> I think quote(survival::coxph) will work in place of as.name() ?
    >>
>> On Mon, Jun 8, 2020 at 3:12 PM G?ran Brostr?m <goran.brostrom at umu.se> wrote:
>>> 
    >>> Hello,
    >>> 
    >>> the function 'coxreg' in my package 'eha' is often just a wrapper for
    >>> 'coxph' in survival, so I have code like
    >>> 
    >>> if (cox.ph){
    >>> Call <- match.call()
    >>> Call[[1]] <- as.name("coxph")
    >>> fit <- eval.parent(Call)
    >>> return(fit)
    >>> }
    >>> 
    >>> which works since eha depends on survival. Now I am thinking of changing
    >>> Depends to Imports, and I would like to change the code to
    >>> 
    >>> if (cox.ph){
    >>> Call <- match.call()
    >>> Call[[1]] <- as.name("survival::coxph")
    >>> fit <- eval.parent(Call)
    >>> return(fit)
    >>> }
    >>> 
    >>> but this doesn't work, because the function name is turned into
    >>> `survival::coxph` (with the backticks) and the evaluation fails.
    >>> 
    >>> How can I solve this?
    >>> 
    >>> Thanks, G,
    >>> 
    >>> ______________________________________________
    >>> R-package-devel at r-project.org mailing list
    >>> https://stat.ethz.ch/mailman/listinfo/r-package-devel

    > ______________________________________________
    > R-package-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-package-devel
#
"pkg::fun" cannot be a name because it is a function call already
`::`(pkg, fun).

On Tue, Jun 9, 2020 at 8:10 AM Martin Maechler
<maechler at stat.math.ethz.ch> wrote: