简体   繁体   English

包中的R source命令

[英]R source command in package

How can I source my function files in r-packages? 如何在r-packages中获取功能文件?

Example (tree dir of my package, package name is "pack"): 示例(我的包的树目录,包名称为“ pack”):

- man (help files)
- R
 -- pack.function1.R
 -- pack.function2.R
 -- ...
- myfunctions
 -- functions.R
DESCRIPTION
NAMESPACE
...


functions.R
foo <- function(bar) {
   return(bar)
}


pack.function1.R
...
source("myfunctions/functions.R")
foo(bar)

I tried "myfunctions/functions.R", "/myfunctions/functions.R", "../myfunctions/functions.R" but it does work. 我尝试了“ myfunctions / functions.R”,“ / myfunctions / functions.R”,“ ../ myfunctions / functions.R”,但它确实起作用。

How is the correct source path? 正确的源路径如何? It's my first try with R packages. 这是我第一次尝试使用R包。

It looks like you are trying to organize the R code files in your package into different (sub) folders. 您似乎正在尝试将软件包中的R代码文件组织到不同的(子)文件夹中。

This is not supported within R packages. R软件包不支持此功能。

If you install your package you can query the installation path from a function within your package: 如果安装软件包,则可以从软件包中的函数查询安装路径:

system.file(package = "data.table")
[1] "/home/ryoda/R/x86_64-pc-linux-gnu-library/3.4/data.table"

But if you look into the R folder of the installed package you will not find your R code but a "precompiled" image of all your R code (see the *.rdb and *.rdx files). 但是,如果查看已安装软件包的R文件夹,您将找不到您的R代码,而是所有R代码的“预编译”映像(请参阅* .rdb和* .rdx文件)。

Therefore you cannot source other R source code files within R. 因此,您不能在R中获取其他R源代码文件。

For details around the folder structure see: https://cran.r-project.org/doc/manuals/R-exts.html#Data-in-packages 有关文件夹结构的详细信息,请参见: https : //cran.r-project.org/doc/manuals/R-exts.html#Data-in-packages

PS: There is (at least) one "back-door": PS :(至少)有一个“后门”:

If you put R files of your package into a sub folder under the package root named inst the contents of the inst sub folder will be copied recursively to the installation directory and could be source then (use system.file(package = getPackageName()) to retrieve the installation path from within your package. 如果将软件包的R文件放在名为inst的软件包根目录下的子文件夹中,则inst子文件夹的内容将被递归复制到安装目录,并且可能是源目录(请使用system.file(package = getPackageName())从软件包中检索安装路径。

But this is far beyond the way a package should work. 但是,这远远超出了软件包的工作方式。

A valid exception could be: Provide pre-configured configuration files that are pure R files creating variables... 一个有效的例外可能是:提供预配置的配置文件,这些配置文件是创建变量的纯R文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM