How to call an R script from another R script, as in one package?

I am creating a package that uses two main functions. One of the functions of model.R requires a special type of simulation sim.R and a way to tune the results in table.R

In a shared package, how can I call sim.R and table.R from model.R ? I tried source("sim.R") and source("R/sim.R") , but this call does not work from 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, all the DESCRIPTION and NAMESPACE files are installed. I just have some scripts in the directory R. ~ R / has premodel.R model.R sim.R and table.R . I need a model.R script to use the functions sim.R and table.R ... located in the same folder in the package (e.g. ~ R /).

+8
function r package
source share
4 answers

To clarify the joran point, when you create the package, you do not need the original functions.

For example, imagine that I want to make a package called TEST. I will start by creating a directory (i.e. Folders) named TEST. As part of the TEST, I will create a different R folder name, in this folder I will include all R script (s) containing various functions in the package.

At a minimum, you need to include the DESCRIPTION and NAMESPACE files as well. It is also nice to include a person (for help files) and tests (for unit tests).

Creating a package is pretty simple. Here is a blog with a simple introduction: http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/

+2
source share

As others have pointed out, you do not need to place R files in a package. The package loading mechanism will take care of losing the namespace and making all exported functions available. Therefore, usually you do not need to worry about it.

However, there are exceptions. If you have several files with R-code, situations may arise when the order in which these files are processed matters. Often this does not matter, or the default order used by R is in order. If you find that there are some dependencies in your package that are not resolved properly, you may encounter a situation where you need a custom order for R files. For this purpose, the DESCRIPTION file offers an optional Collate field. Just list all your R files in the order in which they should be processed to satisfy the dependencies.

0
source share

Since you are creating the package, the reason you are having problems accessing other functions in the /R directory is because you need to first:

 library(devtools) document() 

from the working directory of your package. Now every function in your package should be available for any other function. Then, to finish, do:

 build() install() 

although it should be noted that a simple call to document() will already be enough to solve your problem.

0
source share

If all your files are in the R directory, any function will be in memory after creating the package assembly or Load_All . You may have problems if you have code in files that are not in the tho function.

R downloads files in alphabetical order.

This is usually not a problem, because functions are evaluated when they are called for execution, and not during loading (id. A function can refer to another function that is not yet defined, even in the same file).

But if you have code outside the function in model.R, this code will be executed immediately when the file is downloaded, and your package assembly will not normally be performed using

 ERROR: lazy loading failed for package 'yourPackageName' 

If so, put the sparse model.R code in the function so that you can call it later when the package is fully loaded, the external library too.

If this piece of code exists to initialize some value, consider use_data() so that R takes care of loading the data into the environment for you. If this piece of code is just interactive code written for testing and implementing the package itself, you should consider placing it in another place or in any case passing it to functions.

if you really need this code that will be executed at boot time or will really need a solution, then you must add the mapping string to the DESCRIPTION file, as already mentioned by Peter Humburg, to make R load the file. Roxygen2 can help you put code before

 #' @include sim.R table.R 

call roxygenize() and the sort string will be generated for you in the DESCRIPTION file.

But even so, an external library that you may depend on is not yet loaded by the package, which leads to a failure again during build.

In conclusion, you better not leave the code outside the functions in the .R file if it is inside the package.

0
source share

All Articles