I have an S3 generic function that I would like to be an internal part of a package. I would prefer not to export it if possible. An interesting downside of this is that it seems that lapply
is unable to find or use the proper S3 method. Does anyone know the reason behind this behavior? below is a reproducible example which involves installing a dummy package from my github.
In this case, the generic function is docheck
which will return TRUE
if the object is "foo"
and FALSE
otherwise.
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)
The foo function is just an NA
object with the "foo"
class.
foo
#> function ()
#> structure(NA, class = "foo")
#> <bytecode: 0x0000000015033878>
#> <environment: namespace:SOExample>
fun_success()
just shows a case in which the internal (not exported) S3 function works.
fun_success
#> function (x)
#> {
#> docheck(x)
#> }
#> <bytecode: 0x0000000013f83100>
#> <environment: namespace:SOExample>
fun_success(foo())
#> [1] TRUE
fun_failure()
calls the same internal generic method, but fails
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)
lapply
is unable to find the proper method. Maybelapply
evaluates in a new environment? – Thermometer