Weird execution files on library, elements not found

Hi all, I'm writing a lib, but then I found something pretty weird, some cases the element does not exists, so:

In a empty lib do the next thing.

R/def.R

a <- 10

R/cpp.R

b <- a

Now, when you do devtools::load_all("."), you will get:

object 'a' not found

But, if you rename cpp.R to set.R then it will works.

What I think about this, is that R loads the files from R/* in alphabet order, causing to call first cpp.R and then def.R, how a does not exists sends an error.

Is there a way to handle this in a good way?

Thx!

Can you explain a bit why you want to have non-function objects exported? My impression is that this is not really something you'd do with packages, there are various ways to store and load data, and you'd typically want to be explicit, for example by storing it in the "data/" directory.

Hi, I'm actually using a library called "typed", it helps to set types of vars and return functions, the problem is the order of execution of the R files, because a single definition, like "a dataframe with this column" is defined, then used in several files and functions, how they are called when the function is created, request to first exist the declaration....

Need a way to force R to read first a file, to then continue loading the next one in the lib.

Then the easiest might be to use the Collate field of DESCRIPTION, as explained here.

So for example, in cpp.R you can use:

#' @include def.R
b <- a

then run

roxygen2::update_collate(".")
devtools::load_all()
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.