如何从同一个包中的另一个R脚本调用R脚本?

时间:2022-05-07 16:54:59

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

我正在构建一个使用两个主要功能的包。其中一个函数model.R需要一种特殊类型的模拟sim.R和一种在表格中设置结果的方法.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?

在可共享的包中,如何从model.R中调用sim.R和table.R文件?我已经尝试过source(“sim.R”)和source(“R / sim.R”),但是这个调用在包中不起作用。有任何想法吗?

Should I just copy and paste the codes from sim.R and table.R into the model.R script instead?

我应该将sim.R和table.R中的代码复制并粘贴到model.R脚本中吗?

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/).

编辑:我有R目录中的所有脚本,DESCRIPTION和NAMESPACE文件都已设置。我只在R目录中有多个脚本。 ~R /有预模型.R模型.R sim.R和table.R。我需要model.R脚本来使用sim.R和table.R函数......位于包中的同一目录中(例如~R /)。

5 个解决方案

#1


2  

To elaborate on joran's point, when you build a package you don't need to source functions.

要详细说明joran的观点,当您构建一个包时,您不需要提供函数。

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.

例如,想象一下我想创建一个名为TEST的包。我将首先生成一个名为TEST的目录(即文件夹)。在TEST中,我将创建另一个文件夹名称R,在该文件夹中,我将包含包含包中不同功能的所有R脚本。

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.

您至少还需要包含DESCRIPTION和NAMESPACE文件。一个人(用于帮助文件)和测试(用于单元测试)也很好。

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/

制作包装非常简单。这是一个简单介绍的博客:http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/

#2


0  

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.

正如其他人指出的那样,您不必在包中获取R文件。包加载机制将负责丢失命名空间并使所有导出的函数可用。所以通常你不必担心这些。

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.

但也有例外。如果您有多个带有R代码的文件,则可能会出现处理这些文件的顺序很重要的情况。通常它无关紧要或R使用的默认顺序恰好是正常的。如果您发现包中存在一些未正确解析的依赖项,则可能会遇到需要R文件的自定义处理顺序的情况。 DESCRIPTION文件为此提供了可选的Collat​​e字段。只需按照处理顺序列出所有R文件以满足依赖关系。

#3


0  

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:

由于您正在构建程序包,因此您无法访问/ R目录中的其他函数是因为您需要首先:

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.

虽然应该注意一个简单的document()调用已经足以解决您的问题。

#4


0  

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目录中,则在执行包构建或Load_All后,任何函数都将在内存中。如果您的文件中的代码不在函数中,则可能会出现问题。

R loads files in alphabetical order.

R按字母顺序加载文件。

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).

通常,这不是问题,因为函数在被调用执行时被评估,而不是在加载时(id。函数可以引用尚未定义的另一个函数,即使在同一文件中)。

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

但是,如果你在model.R中的函数之外有代码,这个代码将在文件加载时立即执行,你的包构建通常会失败

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.

如果是这种情况,请将model.R的稀疏代码包装到函数中,以便稍后在包完全加载时调用它,外部库也可以。

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.

如果这段代码用于初始化某个值,请考虑使用use_data()让R负责将数据加载到环境中。如果这段代码只是为了测试和实现包本身而编写的交互式代码,那么您应该考虑将其放在别处或者将其包装到函数中。

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

如果你真的需要在加载时执行代码或者真的有依赖来解决,那么你必须将整理行添加到DESCRIPTION文件中,如Peter Humburg所说,强制R加载文件顺序。 Roxygen2可以帮助你,放在你的代码之前

#' @include sim.R table.R

call roxygenize(), and collate line will be generate for you into the DESCRIPTION file.

调用roxygenize(),并为您生成整理行到DESCRIPTION文件中。

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.

总之,如果代码位于包内,最好不要将代码保留在.R文件中。

#5


0  

Make your functions global by defining them with <<- instead of <- and they will become available to any other script running in that environment.

通过使用<< - 而不是< - 定义它们使函数全局化,并且它们将可用于在该环境中运行的任何其他脚本。

#1


2  

To elaborate on joran's point, when you build a package you don't need to source functions.

要详细说明joran的观点,当您构建一个包时,您不需要提供函数。

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.

例如,想象一下我想创建一个名为TEST的包。我将首先生成一个名为TEST的目录(即文件夹)。在TEST中,我将创建另一个文件夹名称R,在该文件夹中,我将包含包含包中不同功能的所有R脚本。

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.

您至少还需要包含DESCRIPTION和NAMESPACE文件。一个人(用于帮助文件)和测试(用于单元测试)也很好。

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/

制作包装非常简单。这是一个简单介绍的博客:http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/

#2


0  

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.

正如其他人指出的那样,您不必在包中获取R文件。包加载机制将负责丢失命名空间并使所有导出的函数可用。所以通常你不必担心这些。

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.

但也有例外。如果您有多个带有R代码的文件,则可能会出现处理这些文件的顺序很重要的情况。通常它无关紧要或R使用的默认顺序恰好是正常的。如果您发现包中存在一些未正确解析的依赖项,则可能会遇到需要R文件的自定义处理顺序的情况。 DESCRIPTION文件为此提供了可选的Collat​​e字段。只需按照处理顺序列出所有R文件以满足依赖关系。

#3


0  

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:

由于您正在构建程序包,因此您无法访问/ R目录中的其他函数是因为您需要首先:

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.

虽然应该注意一个简单的document()调用已经足以解决您的问题。

#4


0  

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目录中,则在执行包构建或Load_All后,任何函数都将在内存中。如果您的文件中的代码不在函数中,则可能会出现问题。

R loads files in alphabetical order.

R按字母顺序加载文件。

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).

通常,这不是问题,因为函数在被调用执行时被评估,而不是在加载时(id。函数可以引用尚未定义的另一个函数,即使在同一文件中)。

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

但是,如果你在model.R中的函数之外有代码,这个代码将在文件加载时立即执行,你的包构建通常会失败

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.

如果是这种情况,请将model.R的稀疏代码包装到函数中,以便稍后在包完全加载时调用它,外部库也可以。

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.

如果这段代码用于初始化某个值,请考虑使用use_data()让R负责将数据加载到环境中。如果这段代码只是为了测试和实现包本身而编写的交互式代码,那么您应该考虑将其放在别处或者将其包装到函数中。

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

如果你真的需要在加载时执行代码或者真的有依赖来解决,那么你必须将整理行添加到DESCRIPTION文件中,如Peter Humburg所说,强制R加载文件顺序。 Roxygen2可以帮助你,放在你的代码之前

#' @include sim.R table.R

call roxygenize(), and collate line will be generate for you into the DESCRIPTION file.

调用roxygenize(),并为您生成整理行到DESCRIPTION文件中。

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.

总之,如果代码位于包内,最好不要将代码保留在.R文件中。

#5


0  

Make your functions global by defining them with <<- instead of <- and they will become available to any other script running in that environment.

通过使用<< - 而不是< - 定义它们使函数全局化,并且它们将可用于在该环境中运行的任何其他脚本。