Skip to content

[R-pkg-devel] Advice on elegant way to alias function name

11 messages · Neal Fultz, Uwe Ligges, Serguei Sokol +4 more

#
Hi,

I've a package where it has been suggested that one of the functions -- call
it "myfn()" -- should be called something else, say "thefn()". Of course, I'll
need to keep the old name around for a while.

Web search has suggested simple assignment of

     thefn <- myfn

but I cannot seem to get this to work with R CMD check when I put this in a .R
file in the code and put alias and usage stanzas in documentation. I get alias
and missing argument type errors. I've tried a number of variations on this theme
without appreciable success.

A workaround is to copy the entire function with Roxygen2 documentation and
name change, but this seems inelegant.

Is there a better way e.g., using something like onLoad ? Pointers to working
examples in CRAN or Github packages would be welcome.

Best, JN
#
Many of Hadley Wickhams packages have things with both US and UK spellings
(summarise/summarize, color/colour), I would check ggplot2 or dplyr.

https://github.com/cran/dplyr/blob/master/R/summarise.R#L122-L124
On Thu, Sep 1, 2022 at 7:48 AM J C Nash <profjcnash at gmail.com> wrote:

            

  
  
#
On 01.09.2022 16:48, J C Nash wrote:
Can you make the package available? It should work.

Best,
Uwe
#
Le 01/09/2022 ? 16:48, J C Nash a ?crit?:
Is there any place where we can see what alias and stanzas you have used 
actually?

Best,
Serguei.
#
On Thu, Sep 1, 2022 at 9:48 AM J C Nash <profjcnash at gmail.com> wrote:
You'll also need to add a couple of roxygen tags:

#' @export
#' @rdname myfn
thefn <- myfn

Hadley
#
Thanks to all for many quick replies.

In trying different suggestions, it appears I may have some sort of conflict
going on in the Roxygen tags and some other part of my package. To avoid
sowing confusion on the list, I'll take a step back and go through things
carefully and post my findings in a few days, hopefully with an explanation
of why things didn't work as I expected.

Best,

JN
1 day later
#
With some playing around I found a workable solution.

- NAMESPACE needs  export(thefn)
- thefn.Rd needs usage and arguments documented
- myfn.Rd must NOT have alias(thefn)

And I believe I've got the roxygen2 tags to work in Rstudio. In R itself,
roxygen2 gives an error
     Error: invalid version specification ?0.68?
though the string '0.68' appears nowhere in my package according to grep.
I have no idea what this is about, especially as Rstudio works fine.

While I like the idea of documentation in code files, I think I'll go
back to separately created .Rd files.

If anyone is interested, package and README are at
https://gitlab.com/nashjc/nlsr2022

Best,

John Nash
On 2022-09-01 11:29, Hadley Wickham wrote:
#
On 2 September 2022 at 16:22, J C Nash wrote:
| And I believe I've got the roxygen2 tags to work in Rstudio. In R itself,
| roxygen2 gives an error
|      Error: invalid version specification ?0.68?
| though the string '0.68' appears nowhere in my package according to grep.
| I have no idea what this is about, especially as Rstudio works fine.

An unrelated, documented, yet unfixed bug in 'cli' affecting packages using it.

You need to define (in .Renviron, or via bash for your login, or system wide)
the variable VTE_VERSION with any value, for example '1' will do.

E.g. I call roxygen on the shell via 'roxy.r' from littler and my command
history has a few

   VTE_VERSION=1 roxy.r

It also bit me in 'rhub' and other packages communicating via 'cli'.

Dirk
#
On 2022-09-02 4:34 p.m., Dirk Eddelbuettel wrote:
Yes, this one is cryptic and frustrating.

https://github.com/r-lib/devtools/issues/2439#issuecomment-1174146075

   You can also solve the problem by installing the development version 
of cli from Github (the fix was made *one day* after the last CRAN 
release ...)
#
On Fri, Sep 2, 2022 at 3:22 PM J C Nash <profjcnash at gmail.com> wrote:
All you need is:

#' @export
#' @rdname wrapnlsr
nlsr <- wrapnlsr

That will document nlsr and nlsr together in the same file.

In general, if you find yourself having to use `@usage` or `@aliases`
to get R CMD check to pass cleanly, something is off with the way
you're documenting your functions.

Hadley
#
Hadley is correct. I had a typo in one of the tags.

And the @usage tag is not needed.

Thanks to all.

JN
On 2022-09-02 17:46, Hadley Wickham wrote: