Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in unregister for a package that contains an S3 method when there is an object in the global environment with the same name #273

Open
AlexisDerumigny opened this issue Apr 8, 2024 · 0 comments

Comments

@AlexisDerumigny
Copy link

Thanks for the package, it is very useful! As I was developing a package, I encountered some issues with roxygen2 which I tracked down to pkgload::unregister.

When unregistering S3 methods, an error occur if there exists already an object in the global environment with the same name as the S3 method that pkgload is trying to unregister.
The problem is at these lines:

pkgload/R/unload.R

Lines 135 to 140 in 7556a3f

methods <- ns_defs$S3methods[, 1:2, drop = FALSE]
for (i in seq_len(nrow(methods))) {
method <- methods[i, , drop = FALSE]
generic <- env_get(ns, method[[1]], inherit = TRUE, default = NULL)

In my case, I defined a log-likelihood method for an object of class A called logLik.A but I had already an object called logLik in the environment. I solved the problem by cleaning up the environment, but I think this bug can be solved by testing whether generics is a function just after line 140.

Here is a reprex that shows this issue (tested on Windows 10, R 4.3.3, pkgload 1.3.4).
The expected behavior is that there is no error, or that there is a more explicit error pointing to the name logLik which hides the S3 method with the same name.

mydir = normalizePath(tempdir())
package.skeleton(path = mydir)
#> Creating directories ...
#> Creating DESCRIPTION ...
#> Creating NAMESPACE ...
#> Creating Read-and-delete-me ...
#> Saving functions and data ...
#> Making help files ...
#> Done.
#> Further steps are described in 'C:\Users\aderumigny\AppData\Local\Temp\Rtmp0Yz0js/anRpackage/Read-and-delete-me'.
# Removing documentation to make it later with roxygen.
unlink(paste0(normalizePath(mydir), "\\anRpackage\\NAMESPACE"))
unlink(paste0(normalizePath(mydir), "\\anRpackage\\man"), recursive = TRUE)

dir.create(paste0(mydir, "\\anRpackage\\R"))
file.create(paste0(mydir, "\\anRpackage\\R\\a.R"))
#> [1] TRUE
cat("
#' An S3 method
#' @export
logLik.A <- function(object, ...){
  return (0)
}
", file = paste0(mydir, "\\anRpackage\\R\\a.R"))
logLik = 0


roxygen2::roxygenize(package.dir = paste0(normalizePath(mydir), "\\anRpackage"))
#> First time using roxygen2. Upgrading automatically...
#> Setting `RoxygenNote` to "7.3.1"
#> ✖ roxygen2 requires "Encoding: UTF-8"
#> ℹ Current encoding is NA
#> ℹ Loading anRpackage
#> Writing 'NAMESPACE'
#> Writing 'logLik.A.Rd'

pkgload::unregister("anRpackage")
#> Error in `fn_env()`:
#> ! `fn` must be a function, not the number 0.

rlang::last_trace()
<error/rlang_error>
Error in `fn_env()`:
! `fn` must be a function, not the number 0.
---
Backtrace:1. └─pkgload::unregister("anRpackage")
 2.   └─pkgload:::unregister_methods(package)
 3.     └─pkgload:::s3_unregister(package)
 4.       ├─base::topenv(fn_env(generic))
 5.       └─rlang::fn_env(fn = generic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant