Skip to content

BUG(?): Function with class attribute set becomes \keyword{datasets} Rd document #455

@HenrikBengtsson

Description

@HenrikBengtsson

Problem

I'm trying to add a class attribute to a function, but when I do, roxygen2 5.0.1 gets confused and documents it as an R object. For example:

#' Foo function
#'
#' @export
foo <- function() TRUE
class(foo) <- "foo"

generates

% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/foo.R
\docType{data}
\name{foo}
\alias{foo}
\title{Foo function}
\format{An object of class \code{foo} of length 1.}
\usage{
foo
}
\description{
Foo function
}
\keyword{datasets}

Workaround

By making sure the class attribute also contains "function", it seems to work, e.g.

#' Foo function
#'
#' @export
foo <- function() TRUE
class(foo) <- c("foo", "function")

becomes

% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/foo.R
\name{foo}
\alias{foo}
\title{Foo function}
\usage{
foo()
}
\description{
Foo function
}

Troubleshooting

Could it be that roxygen2 identifies functions using inherits(x, "function") rather than is.function(x)? For example:

> foo <- function() TRUE; class(foo) <- "foo"
> inherits(foo, "function")
[1] FALSE
> is.function(foo)
[1] TRUE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions