[R-pkg-devel] S3 method, useMethod and <-, using Roxygen

Daniel Lüdecke d.luedecke at uke.de
Fri Oct 23 14:32:40 CEST 2015


 > which I believe is correct, although I don't think I've ever seen such
 > a construct in the wild.

What would be the proper way to define such methods?

Eg, I want `set_label` to work like this:
x <- set_label(x, "my label")

and additionally, like this:
set_label(x) <- "my label"


How would I define the latter function?

(this is how it is currently done by me)

#' @export
set_label <- function(x, lab, attr.string = NULL) {
...
}

#' @rdname set_label
#' @export
`set_label<-` <- function(x, attr.string = NULL, value) {
   UseMethod("set_label<-")
}

#' @rdname set_label
#' @export
`set_label<-.default` <- function(x, attr.string = NULL, value) {
   x <- set_label(x, value, attr.string)
   x
}




Am 23.10.2015 um 13:10 schrieb Hadley Wickham:
> Here's a minimal reprex:
>
> out <- roc_proc_text(rd_roclet(), "
>    #' Foo
>    `foo<-` <- function(x, y, value) {
>      UseMethod('set_labels<-')
>    }
>
>    #' Foo
>    `foo<-.default` <- function(x, y, value) {
>      x
>    }
>    ")[[2]]
>
> cat(format(out))
>
> That generates
>
> \usage{
> \method{foo}{default}(x, y) <- value
> }
>
--

_____________________________________________________________________

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Prölß, Rainer Schoppik
_____________________________________________________________________

SAVE PAPER - THINK BEFORE PRINTING


More information about the R-package-devel mailing list