Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export S3method both as method and normal function using roxygen2

There are similar, older questions out there, but since roxygen2 version 3.0.0 things have changed a bit (as I understand from other Q&A here on SO).

I have written an alternative function format.Date, which I want to export, both as method and as function.
Using the @export tag means roxygen2 recognises it as an S3-method for print, and registers it accordingly. And when I load my package, and print a date object, my method gets called. So far, so good.

But, when I then call format.Date, I still get the normal base-method. This also happens when I use debugonce(format.Date), the 'debug-mark' gets set on base::format.Date, so if my method gets called, nothing happens. Or if I want to inspect the source-code: very hard for a user to understand that what he sees with View(format.Date) is NOT what is executed.
And if a user looks into my package what functions I have provided, format.Date is not there.

So I want format.Date to be exported as both an S3-method, and as a normal function called format.Date. In order to do so, I expect my NAMESPACE file to contain both following lines:

S3method(format,Date)
export(format.Date)

Is this possible in roxygen2? I get the impression you could do this in earlier versions (as you could supply both @S3method/@method and @export), but I can't get it to work now.

Background-info: roxygen2 version 6.1.1 with R 3.5.1, run under Rstudio 1.1.453/MacOS 10.13.6

like image 916
Emil Bode Avatar asked Nov 18 '25 06:11

Emil Bode


1 Answers

The ways I found are

  1. @exportS3Method and an explicit @export line. Since roxygen2 changes often, this might change in the future:

    #' @exportS3Method fortify
    #' @export fortify.Date
    
  2. Manually spelling out the NAMESPACE content (add no other @export or @method directives)

    #' @rawNamespace S3method(fortify,Date)
    #' export(fortify.Date)
    

Both will result in the NAMESPACE file containing the following lines, with the first one resulting in roxygen2 ordering things for you.

S3method(fortify,Date)
export(fortify.Date)
like image 135
flying sheep Avatar answered Nov 19 '25 23:11

flying sheep