Reputation: 23747
Does anyone have an explanation for the following behaviour?
I have a .R file for documentation. I want to use an internal object for creation of a new object (imported or exported, this does not matter, both result in the same failure)
For my package testpak
, I have created an internal object with
data_int <- data.frame(a = 1)
usethis::use_data(data_int, internal = TRUE, overwrite = TRUE)
To build the package, I use an .R file with the following code:
Does not work
#' some_name
#' @description something
#' @return list for each test type
test <- testpak:::data_int
#> Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : object 'data_int' not found
#> ERROR: lazy loading failed for package ‘testpak’
I kind of get the point that somehow there is trouble to find the object in the not yet defined namespace.
But now it gets funny. When I access the same object within a function, the package build works.
#' some_name
#' @description something
#' @return list for each test type
test <- function() testpak:::data_int
#> no error
Another way to make the package build work is by adding usethis::use_data(...)
to the code.
Funny enough, it does not actually overwrite the previously written internal object ! (despite using overwrite = TRUE
)
#' some_name
#' @description something
#' @return list for each test type
data_int <- data.frame(a = 1)
usethis::use_data(data_int, internal = TRUE, overwrite = TRUE) # does not actually overwrite the previously written internal object
test <- testpak:::data_int
#> no error
sessionInfo()
#R version 3.6.3 (2020-02-29)
#Platform: x86_64-apple-darwin15.6.0 (64-bit)
#Running under: macOS Mojave 10.14.6
#roxygen2_7.1.0
Upvotes: 2
Views: 63
Reputation: 44867
If you create a function in the source of your package, it isn't executed. You can create a function like
alwaysError <- function(...) stop("This is an error!")
and it's not going to cause an error until you run it.
Your code
test <- testpak:::data_int
creates the test
object by executing the code on the right. So it generates an error, because testpak
doesn't exist as a package yet, so you can't extract the data_int
object from it.
The usethis::use_data
function is supposed to write some source code into your package. But if you execute it while trying to install your package, it'll probably write that code into some irrelevant place. Apparently wherever it gets written, it's not being read by the package install code.
Upvotes: 2