I believe you can

ex3 <- function() {
  counts <- c(18,17,15,20,10,20,25,13,12)
  outcome <- gl(3,1,9)
  treatment <- gl(3,3)
  stats::glm(counts ~ outcome + treatment, family = stats::poisson)
}

I think if family is character it's searching in parent.frame() rather
than package namespace.


~Michal


On Mon, Mar 9, 2026 at 12:54 PM Tim Taylor
<[email protected]> wrote:
>
> I appreciate there are likely many places where calling a stats function via 
> `::` and without the stats package being loaded could be problematic but 
> would R core have any interest in adapting functions to accommodate this 
> where possible?
>
> The example I ran in to today can be seen below:
>
> ex <- function() {
>     counts <- c(18,17,15,20,10,20,25,13,12)
>     outcome <- gl(3,1,9)
>     treatment <- gl(3,3)
>     stats::glm(counts ~ outcome + treatment, family = "poisson")
> }
>
> tools::R(ex)
> #>
> #> Call:  stats::glm(formula = counts ~ outcome + treatment, family = 
> "poisson")
> #>
> #> Coefficients:
> #> (Intercept)     outcome2     outcome3   treatment2   treatment3
> #>   3.045e+00   -4.543e-01   -2.930e-01    6.972e-16    8.237e-16
> #>
> #> Degrees of Freedom: 8 Total (i.e. Null);  4 Residual
> #> Null Deviance:           10.58
> #> Residual Deviance: 5.129     AIC: 56.76
>
> tools::R(ex, env=c("R_DEFAULT_PACKAGES=NULL"))
> #> Error: error in inferior call:
> #>   object 'poisson' of mode 'function' was not found
>
> The second call fails due to the following line in glm:
>
> if (is.character(family))
>         family <- get(family, mode = "function", envir = parent.frame())
>
> A non-breaking patch (AFAICT) could add an additional branch that explicitly 
> searches a lookup of functions in the stats package if the above call to 
> `get` failed.
>
> Again I understand this could very much be a case of, "don't do that", but ...
>
> Regards
>
> Tim
>
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to