尽管存在默认方法,但 S3 通用“没有适用的方法”

问题描述 投票:0回答:1

这很棘手,因为这个问题只发生在包的上下文中——在全局命名空间中定义时,一切都按预期工作。

我定义了一个名为

coerce_na_range()
的 S3 泛型,它有两个方法:
coerce_na_range.factor()
coerce_na_range.default()
coerce_na_range()
已导出,但两个方法均未导出。 (该函数的目的是在给定范围内用
NA
替换编码为字符或因子标签的数字。)

在全局命名空间中定义时,如果我将字符向量传递给

coerce_na_range()
,它会将其分派到
coerce_na_range.default()
并按预期工作:

vec <- c("green", "yellow", "-9", "red", "-1")
coerce_na_range(vec)
# [1] "green"  "yellow" NA       "red"    NA   

但是,如果我在新会话中加载包,它似乎会忽略默认方法:

library(lighthouse)

vec <- c("green", "yellow", "-9", "red", "-1")
coerce_na_range(vec)
# Error in UseMethod("coerce_na_range") : 
#  no applicable method for 'coerce_na_range' applied to an object of class "character"

我不认为问题是方法没有导出?例如,

tidyr:::full_seq.Date()
等不会导出,而
tidyr::full_seq()
显然可以工作。

该软件包托管在 https://github.com/ccsarapas/lighthouse

coerce_na_range()
、其方法以及它们依赖的一些函数的代码是:

#' Suppress NA warning when coercing to numeric
#'
#' Coerces `x` to numeric. If `x` cannot be coerced, returns `NA` and suppresses
#' coercion warning.
#'
#' @export
try_numeric <- function(x) {
  if (is.factor(x)) {
    warning(
      "`x` is a factor and will be converted based on factor codes, not factor labels."
    )
  }
  withCallingHandlers(
    warning = function(w) {
      if (conditionMessage(w) == "NAs introduced by coercion") {
        rlang::cnd_muffle(w)
      }
    },
    as.numeric(x)
  )
}

#' @rdname try_numeric
#'
#' @export
try.numeric <- function(x) try_numeric(x)

#' Generate NA values of appropriate type
#'
#' Returns compatible `NA` based on `x`. This is usually of the same type as `x`
#' (e.g., `NA_real_` if `x` is a double vector). If `x` is a factor, will
#' return `NA_character_` if `factor_as_character = TRUE` (the default) and
#' `NA_integer_` otherwise.
#'
#' @export
na_like <- function(x, factor_as_character = TRUE, match_length = FALSE) {
  stopifnot("`x` must be an atomic vector" = is.atomic(x))
  type_out <- if (factor_as_character && is.factor(x)) "character" else typeof(x)
  length_out <- if (match_length) length(x) else 1L
  rep(methods::as(NA, type_out), length_out)
}


#' Set NA values based on numbers stored as strings.
#'
#' Changes values coercible to numeric in range `range_min`:`range_max` to `NA`.
#' Useful for imported SPSS files.
#'
#' @export
coerce_na_range <- function(x, ...) UseMethod("coerce_na_range")
coerce_na_range.default <- function(x, range_min = -Inf, range_max = -1) {
  coerced <- try.numeric(x)
  dplyr::if_else(
    is.na(coerced) | (coerced < range_min) | (coerced > range_max),
    x,
    na_like(x)
  )
}
coerce_na_range.factor <- function(x, range_min = -Inf, range_max = -1) {
  lvls <- levels(x)
  coerced <- try.numeric(as.character(lvls))
  lvls <- lvls[is.na(coerced) | coerced < range_min | coerced > range_max]
  factor(x, levels = lvls)
}
r r-package roxygen2 r-s3
1个回答
0
投票

@export
之前添加
generic.fact
标签并运行
roxgenize
应该可以修复它:

# [...]
#' @export
coerce_na_range.factor <- function(x, range_min = -Inf, range_max = -1) {
  lvls <- levels(x)
  coerced <- try.numeric(as.character(lvls))
  lvls <- lvls[is.na(coerced) | coerced < range_min | coerced > range_max]
  factor(x, levels = lvls)
}

然后在控制台运行:

roxygen2::roxygenise()

© www.soinside.com 2019 - 2024. All rights reserved.