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

@export generates a wrong NAMESPACE directive for S3 methods of generics imported from a different package #1613

Open
klmr opened this issue Apr 22, 2024 · 0 comments

Comments

@klmr
Copy link
Contributor

klmr commented Apr 22, 2024

When exporting a new S3 method for a generic imported from another package, ‘roxygen2’ 7.3.1 generates an incorrect NAMESPACE directive; namely, it generates export() rather than S3method(). However, this only happens when both of the following conditions are fulfilled:

  1. if the imported package is not attached, and
  2. when there is no NAMESPACE file, i.e. only the first time round: when rerunning roxygen2::roxygenize(), it “corrects” the NAMESPACE file (intermittently I’ve also managed to produce this with an existing NAMESPACE file, but the exact circumstances are unclear to me, and I no longer manage the right circumstances to reproduce this).

Here’s a minimal example: a package without NAMESPACE file with a single implementation file R/foo.r (complete MWE at the end in the details):

#' @importFrom utils head
#' @export
head.foo = function (x, ...) {
  x$head
}

#' @export
foo = function () {
  structure(list(head = 'caput'), class = 'foo')
}

Now run the following code to generate the NAMESPACE file:

detach('package:utils')

roxygen2::roxygenize()
writeLines(readLines('NAMESPACE'))

roxygen2::roxygenize()
writeLines(readLines('NAMESPACE'))
ℹ Loading pkg
Writing 'NAMESPACE'
# Generated by roxygen2: do not edit by hand

export(foo)
export(head.foo)
importFrom(utils,head)
ℹ Loading pkg
Writing 'NAMESPACE'
# Generated by roxygen2: do not edit by hand

S3method(head,foo)
export(foo)
importFrom(utils,head)

Complete MWE
dir.create('pkg/R', recursive = TRUE)

desc = matrix(
  c(
    'pkg', 'Package', 'What the Package Does (Title Case)', '0.1.0',
    'person(\'First\', \'Last\', email = \'last@example.com\', role = c(\'aut\', \'cre\'))',
    'More about what it does.', 'GPL', 'utils', 'UTF-8'
  ),
  ncol = 9L,
  dimnames = list(
    NULL, c('Package', 'Type', 'Title', 'Version', 'Authors@R', 'Description', 'License', 'Imports', 'Encoding')
  )
)
write.dcf(desc, 'pkg/DESCRIPTION')

code = r"[#' @importFrom utils head
#' @export
head.foo = function (x, ...) {
  x$head
}

#' @export
foo = function () {
  structure(list(head = 'caput'), class = 'foo')
}
]"
writeLines(code, 'pkg/R/foo.r')

###

setwd('pkg')
detach('package:utils')

roxygen2::roxygenize()
writeLines(readLines('NAMESPACE'))

roxygen2::roxygenize()
writeLines(readLines('NAMESPACE'))
@klmr klmr changed the title @export generates a wrong NAMESPACE directive for S3 methods for generics imported from a different package @export generates a wrong NAMESPACE directive for S3 methods of generics imported from a different package Apr 22, 2024
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