If functions are loaded and made available to the users they should be runable without any trouble.
Actual Result
If I run attachment::att_amend_desc() my package function are available in the global environment but do not run properly because R cannot resolve their dependencies.
Reprex
Below is the full reprex of the issue.
dummypackage <- tempfile('dummypackage')
suppressMessages(
usethis::create_package(
path = dummypackage,
open = FALSE,
)
)
#> Package: dummypackage17699584f5f07
#> Title: What the Package Does (One Line, Title Case)
#> Version: 0.0.0.9000
#> Authors@R (parsed):
#> * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
#> Description: What the package does (one paragraph).
#> License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
#> license
#> Encoding: UTF-8
#> Roxygen: list(markdown = TRUE)
#> RoxygenNote: 7.2.0
# Add a dummy function using an external dependency
# namely the {magrittr} pipe %>%
r_file <- file.path(dummypackage, "R", "fun.R")
file.create(r_file)
#> [1] TRUE
writeLines(
text = "#' @importFrom magrittr %>%
#' @export
my_length <- function(x) {
x %>% length()
}",
con = r_file
)
# Add {magrittr} in the DESCRIPTION Imports Field
usethis::use_package("magrittr")
#> Error: Path '/tmp/RtmpT8Kiax/reprex-1761e3d8a0014-milky-cub/' does not appear to be inside a project or package.
# Convert Roxygen tags to .Rd
roxygen2::roxygenise(
package.dir = dummypackage
)
#> ℹ Loading dummypackage17699584f5f07
#> Writing 'NAMESPACE'
#> Writing 'NAMESPACE'
# => This fails because the package is loaded before the NAMESPACE is created.
# roxygen2::roxygenise() uses roxygen2::load_pkgload() which by default equals to
# pkgload::load_all(path, helpers = FALSE, attach_testthat = FALSE)$env
# This is because: "roxygen2 is a dynamic documentation system, which means it
# works with the objects inside your package, not just the source code used to create them."
# -> First all functions are loaded then the NAMESPACE is generated.
# my_length() is available in the global environmeent but as there was no NAMESPACE when
# it was loaded it cannot resolve its dependencies.
my_length(1)
#> Error in x %>% length(): could not find function "%>%"
# Re running roxygen2::load_pkgload(), reloads my_length()
# but this time the NAMESPACE does exists, R can use it to
# resolve dependencies and then...
roxygen2::load_pkgload(path = dummypackage)
#> ℹ Loading dummypackage17699584f5f07
#> <environment: namespace:dummypackage17699584f5f07>
# ... my_length() works
my_length(1)
#> [1] 1
# Removing the NAMESPACE
namespace <- file.path(dummypackage, "NAMESPACE")
file.remove(namespace)
#> [1] TRUE
roxygen2::roxygenise(
package.dir = dummypackage
)
#> ℹ Loading dummypackage17699584f5f07
#> Writing 'NAMESPACE'
#> Writing 'NAMESPACE'
# Does not work
my_length(1)
#> Error in x %>% length(): could not find function "%>%"
roxygen2::roxygenise(
package.dir = dummypackage
)
#> ℹ Loading dummypackage17699584f5f07
# Works since roxygen2::roxygenise() was run 2 times
# The NAMESPACE was around when the package was loaded the second time.
my_length(1)
#> [1] 1
# attachment::att_amend_desc() systematically remove the NAMESPACE,
# thus running running this function 2 times wont fix the problem
attachment::att_amend_desc(
path = dummypackage
)
#> Updating dummypackage17699584f5f07 documentation
#> ℹ Loading dummypackage17699584f5f07Writing 'NAMESPACE'Writing 'NAMESPACE'[+] 1 package(s) added: magrittr.
# Does not work
my_length(1)
#> Error in x %>% length(): could not find function "%>%"
attachment::att_amend_desc(
path = dummypackage
)
#> Updating dummypackage17699584f5f07 documentation
#> ℹ Loading dummypackage17699584f5f07Writing 'NAMESPACE'Writing 'NAMESPACE'
# Still does not work
my_length(1)
#> Error in x %>% length(): could not find function "%>%"
attachment::att_amend_desc(
path = dummypackage
)
#> Updating dummypackage17699584f5f07 documentation
#> ℹ Loading dummypackage17699584f5f07Writing 'NAMESPACE'Writing 'NAMESPACE'
roxygen2::load_pkgload(path = dummypackage)
#> ℹ Loading dummypackage17699584f5f07
#> <environment: namespace:dummypackage17699584f5f07>
# works !
my_length(1)
#> [1] 1
# The main culprit is attachment::att_from_namespace()
# This where the NAMESPACE is removed and recreated with roxygen2::roxygenise()
# each time
attachment::att_from_namespace(
path = file.path(dummypackage, "NAMESPACE")
)
#> Updating dummypackage17699584f5f07 documentation
#> ℹ Loading dummypackage17699584f5f07Writing 'NAMESPACE'Writing 'NAMESPACE'
#> [1] "magrittr"
# Does not work
my_length(1)
#> Error in x %>% length(): could not find function "%>%"
roxygen2::load_pkgload(path = dummypackage)
#> ℹ Loading dummypackage17699584f5f07
#> <environment: namespace:dummypackage17699584f5f07>
# works !
my_length(1)
#> [1] 1
Expected Result
If functions are loaded and made available to the users they should be runable without any trouble.
Actual Result
If I run
attachment::att_amend_desc()
my package function are available in the global environment but do not run properly because R cannot resolve their dependencies.Reprex
Below is the full reprex of the issue.
Created on 2022-07-08 by the reprex package (v2.0.1)
Session info
``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23) #> os Pop!_OS 22.04 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Amsterdam #> date 2022-07-08 #> pandoc 2.18 @ /usr/lib/rstudio/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> ! package * version date (UTC) lib source #> attachment 0.2.5.9000 2022-07-08 [1] local #> brio 1.1.3 2021-11-30 [1] CRAN (R 4.2.0) #> cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.0) #> crayon 1.5.1 2022-03-26 [1] CRAN (R 4.2.0) #> desc 1.4.1 2022-03-06 [1] CRAN (R 4.2.0) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0) #> R dummypackage17699584f5f07 * 0.0.0.9000