3

I receive the following note when trying to check my package: Found the following apparent S3 methods exported but not registered: is.nan.data.frame.

Here is my document that I use with roxygen2 to create the package documentation:

#' @title
#' NaN (Not a Number).
#'
#' @description
#' Check whether a value is "Not A Number" (\code{NaN}) in a dataframe.
#'
#' @details
#' [INSERT].
#'
#' @param x Dataframe.
#'
#' @return \code{TRUE} or \code{FALSE}, indicating whether values in a dataframe
#' are Not a Number (\code{NA}).
#'
#' @family dataEvaluations
#'
#' @usage is.nan.data.frame(x)
#'
#' @examples
#' # Prepare Data
#' df <- data.frame(item1 = rnorm(1000), item2 = rnorm(1000), item3 = rnorm(1000))
#' df[sample(1:nrow(df), size = 100), c("item1","item2","item3")] <- NaN
#'
#' # Calculate Missingness-Adjusted Row Sum
#' is.nan.data.frame(df)
#'
#' @seealso
#' \url{https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097}
#'
#' @method is.nan data.frame
#'
#' @export is.nan.data.frame
is.nan.data.frame <- function(x) do.call(cbind, lapply(x, is.nan))

Here is the relevant line from my NAMESPACE file:

export(is.nan.data.frame)

Note that I receive the following warning when running roxygen2:

> library("roxygen2")
> roxygenise()
i Loading petersenlab
Writing NAMESPACE
Writing NAMESPACE
Warning message:
In setup_ns_exports(path, export_all, export_imports) :
  Objects listed as exports, but not present in namespace: data.frame, is.nan

Here is the relevant output when I run the package check:

N  checking S3 generic/method consistency (3.3s)
   Found the following apparent S3 methods exported but not registered:
     is.nan.data.frame
   See section 'Registering S3 methods' in the 'Writing R Extensions'
   manual.
...
   S3 methods shown with full name in documentation object 'is.nan.data.frame':
     'is.nan.data.frame'
   
   The \usage entries for S3 methods should use the \method markup and not
   their full name.

It seems one solution to this is to rename the function to use underscore separators (_) rather than dot separators (.) (How to register methods without referring to the S3 ones). However, I'd like to keep the dot separators (.) if possible so that I can use the is.nan() function with vectors and dataframes (via method dispatch). How can I export this function and get rid of the note?


FYI here is the fixed documentation after resolving the issues (based on the helpful answer and comments)--this no longer throws a note or warning:

#' @title
#' NaN (Not a Number).
#'
#' @description
#' Check whether a value is "Not A Number" (\code{NaN}) in a dataframe.
#'
#' @details
#' [INSERT].
#'
#' @param x Dataframe.
#'
#' @return \code{TRUE} or \code{FALSE}, indicating whether values in a dataframe
#' are Not a Number (\code{NA}).
#'
#' @family dataEvaluations
#'
#' @examples
#' # Prepare Data
#' df <- data.frame(item1 = rnorm(1000), item2 = rnorm(1000), item3 = rnorm(1000))
#' df[sample(1:nrow(df), size = 100), c("item1","item2","item3")] <- NaN
#'
#' # Calculate Missingness-Adjusted Row Sum
#' is.nan(df)
#'
#' @seealso
#' \url{https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097}
#'
#' @method is.nan data.frame
#'
#' @export
is.nan.data.frame <- function(x) do.call(cbind, lapply(x, is.nan))
itpetersen
  • 1,475
  • 3
  • 13
  • 32
  • 1
    You almost never want to use `@export` with a name. Just use `@export` by itself. – Konrad Rudolph Mar 01 '22 at 13:07
  • 1
    It would be helpful to show the related lines from your `NAMESPACE` file. As @KonradRudolph said, your Roxygen lines don't completely make sense. – user2554330 Mar 01 '22 at 13:10
  • I added the related lines from my `NAMESPACE` file to my question above. I still receive warnings when using just `@export` by itself (see my response to the answer by @user2554330 : https://stackoverflow.com/a/71308978/2029527.) – itpetersen Mar 01 '22 at 13:29
  • OK, your `NAMESPACE` file contains an incorrect entry. Instead of `export(…)` it should contain `S3method(is.nan, "data.frame")`. Odd. You can try explicitly using the `@S3method` Roxygen2 tag, even though in principle you shouldn’t need to. – Konrad Rudolph Mar 01 '22 at 13:30
  • I think the issue had to do with my references in the help page example and `usage` sections to the full name (`is.nan.data.frame()`) rather than to the S3 method (`is.nan()`). Once I removed those, the warning disappeared and the NAMESPACE looks correct: `S3method(is.nan,data.frame)`. Thanks! – itpetersen Mar 01 '22 at 13:39

1 Answers1

3

I don't know Roxygen2 very well, but it appears that you have declared is.nan.data.frame to be the is.nan method for class data.frame. Since you did that, you should call it as is.nan(df) in the help page example.

If you don't want it to be the method, you just want it to be a regular function using dots in the name, then you shouldn't have @method is.nan data.frame. But you indicate that you do want it to be a method.

Edited to add: Just to summarize your comments, the following fixes got rid of all the errors:

  • use @export by itself without naming the function (as suggested by @KonradRudolph)
  • remove the @usage line
  • use is.nan(df) in the example (as I suggested)
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • When I use `@export` by itself (as suggested by @KonradRudolph in the comment above) and change the help page example to `is.nan()`, I receive the following warnings: `S3 methods shown with full name in documentation object 'is.nan.data.frame': 'is.nan.data.frame'`. Here is the relevant line in `NAMESPACE`: `S3method(is.nan,data.frame)` – itpetersen Mar 01 '22 at 13:28
  • Once I removed the `@usage` line (which had the full `is.nan.data.frame` reference, the warning disappeared. So, your fix (in addition to just using `@export` by itself) fixed the issue. Many thanks! – itpetersen Mar 01 '22 at 13:37
  • Interesting, I would not have expected the `@usage` declaration to influence how ‘roxygen2’ generates the namespace export declaration. @itpeterson, if you *want* to include the `@usage` line, it’s worth trying to wrap the code inside `\special{…}`. That way, it’s marked as non-valid R code and ‘roxygen2’ *should* ignore it (but still print your usage declaration, verbatim). – Konrad Rudolph Mar 01 '22 at 14:13
  • That said, as written in this answer, it’s not usual for users to want to use S3 methods by their full name, and they’re not generally exported as such. That is, it’s expected that usage would be `is.nan(df)` instead of `is.nan.data.frame(df)`. – Konrad Rudolph Mar 01 '22 at 14:15