When a package is built, every file in the R/ subdirectory is run, and then the objects they've created are part of the package. By default, you shouldn't assume they're run in a specific order. And usually, that's fine. Let's say we have a package with two .R files:
# R/aardvark.R
aardvark <- function(x) {
zebra(x)
}
# R/zebra.R
zebra <- function(x) {
x + 1
}
Even if aardvark.R is run before zebra.R, the package will build just fine. By the time anything calls the aardvark() function, zebra() has already been defined.
If you need to have the files run in a certain order, you can use the Collate field in the DESCRIPTION file. The official Writing R Extensions describes how this works. That manual's the primary guide for package creation rules, and Hadley's guide (mentioned by @heramb) is an easier read offering good suggestions.
As to whether you should put all the functions in a single script or spread them out: do whatever works best for you. Personally, I have two types of .R files in my package:
- Categories (e.g.,
text-formatting.R). These contain a bunch of functions and other data that have a similar purpose.
- Major objects (e.g.,
query_website.R). These files are named after important functions or datasets in the package and also contain all the "helper" functions used to define them.
Your choice could also be affected by which version control system you use, if/how you collaborate with others, whether you like scrolling or switching windows to go between code, etc. Choose whatever makes you most productive.
Good luck in creating the package! It's definitely overwhelming at first, but it's totally worth it and addictive.