我有一个 S3 通用函数,我希望将其作为包的内部部分。如果可能的话我宁愿不导出它。一个有趣的缺点是,似乎lapply
无法找到或使用正确的 S3 方法。有谁知道这种行为背后的原因?下面是一个可重现的示例,其中涉及从我的 github 安装虚拟包。
在这种情况下,通用函数是docheck
这将返回TRUE
如果该物体是"foo"
and FALSE
否则。
library(remotes)
install_github("jtlandis/SOExample")
#> Skipping install of 'SOExample' from a github remote, the SHA1 (27ab918c) has not changed since last install.
#> Use `force = TRUE` to force installation
library(SOExample)
foo 函数只是一个NA
对象与"foo"
class.
foo
#> function ()
#> structure(NA, class = "foo")
#> <bytecode: 0x0000000015033878>
#> <environment: namespace:SOExample>
fun_success()
仅显示内部(未导出)S3 功能工作的情况。
fun_success
#> function (x)
#> {
#> docheck(x)
#> }
#> <bytecode: 0x0000000013f83100>
#> <environment: namespace:SOExample>
fun_success(foo())
#> [1] TRUE
fun_failure()
调用相同的内部泛型方法,但失败
fun_failure
#> function (x)
#> {
#> lapply(list(x), docheck)
#> }
#> <bytecode: 0x0000000015e68df0>
#> <environment: namespace:SOExample>
fun_failure(foo())
#> Error in UseMethod("docheck"): no applicable method for 'docheck' applied to an object of class "foo"
Created on 2021-07-24 by the reprex package (v2.0.0)