Reputation: 181
I'm building a package that uses two main functions. One of the functions model.R
requires a special type of simulation sim.R
and a way to set up the results in a table table.R
In a sharable package, how do I call both the sim.R
and table.R
files from within model.R
? I've tried source("sim.R")
and source("R/sim.R")
but that call doesn't work from within the package. Any ideas?
Should I just copy and paste the codes from sim.R
and table.R
into the model.R
script instead?
Edit:
I have all the scripts in the R directory, the DESCRIPTION and NAMESPACE files are all set. I just have multiple scripts in the R directory. ~R/ has premodel.R
model.R
sim.R
and table.R
. I need the model.R
script to use both sim.R
and table.R
functions... located in the same directory in the package (e.g. ~R/).
Upvotes: 13
Views: 11416
Reputation: 1
Make your functions global by defining them with <<- instead of <- and they will become available to any other script running in that environment.
Upvotes: 0
Reputation: 46
If all your files are in R directory, any function will be in memory after you do a package build or Load_All. You may have issues if you have code in files that is not in a function tho.
R loads files in alphabetical order.
Usually, this is not a problem, because functions are evaluated when they are called for execution, not at loading time (id. a function can refer another function not yet defined, even in the same file).
But, if you have code outside a function in model.R, this code will be executed immediately at time of file loading, and your package build will fail usually with a
ERROR: lazy loading failed for package 'yourPackageName'
If this is the case, wrap the sparse code of model.R into a function so you can call it later, when the package has fully loaded, external library too.
If this piece of code is there for initialize some value, consider to use_data()
to have R take care of load data into the environment for you.
If this piece of code is just interactive code written to test and implement the package itself, you should consider to put it elsewhere or wrap it to a function anyway.
if you really need that code to be executed at loading time or really have dependency to solve, then you must add the collate line into DESCRIPTION file, as already stated by Peter Humburg, to force R to load files order. Roxygen2 can help you, put before your code
#' @include sim.R table.R
call roxygenize()
, and collate line will be generate for you into the DESCRIPTION file.
But even doing that, external library you may depend are not yet loaded by the package, leading to failure again at build time.
In conclusion, you'd better don't leave code outside functions in a .R file if it's located inside a package.
Upvotes: 1
Reputation: 3024
Since you're building a package, the reason why you're having trouble accessing the other functions in your /R
directory is because you need to first:
library(devtools)
document()
from within the working directory of your package. Now each function in your package should be accessible to any other function. Then, to finish up, do:
build()
install()
although it should be noted that a simple document()
call will already be sufficient to solve your problem.
Upvotes: 0
Reputation: 549
As others have pointed out you don't have to source R files in a package. The package loading mechanism will take care of losing the namespace and making all exported functions available. So usually you don't have to worry about any of this.
There are exceptions however. If you have multiple files with R code situations can arise where the order in which these files are processed matters. Often it doesn't matter or the default order used by R happens to be fine. If you find that there are some dependencies within your package that aren't resolved properly you may be faced with a situation where a custom processing order for the R files is required. The DESCRIPTION file offers the optional Collate field for this purpose. Simply list all your R files in the order they should be processed to satisfy the dependencies.
Upvotes: 1
Reputation: 4513
To elaborate on joran's point, when you build a package you don't need to source functions.
For example, imagine I want to make a package named TEST. I will begin by generating a directory (i.e. folder) named TEST. Within TEST I will create another folder name R, in that folder I will include all R script(s) containing the different functions in the package.
At a minimum you need to also include a DESCRIPTION and NAMESPACE file. A man (for help files) and tests (for unit tests) are also nice to include.
Making a package is pretty easy. Here is a blog with a straightforward introduction: http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/
Upvotes: 5