让R在库目录中查找文件

时间:2023-01-18 07:39:04

I am using R, on linux. I have a set a functions that I use often, and that I have saved in different .r script files. Those files are in ~/r_lib/.

我在linux上使用R。我有一个经常使用的函数集,并且我保存在不同的.r脚本文件中。那些文件在〜/ r_lib /中。

I would like to include those files without having to use the fully qualified name, but just "file.r". Basically I am looking the same command as -I in the c++ compiler.

我想包含这些文件,而不必使用完全限定的名称,但只是“file.r”。基本上我在c ++编译器中看起来与-I相同的命令。

I there a way to set the include file from R, in the .Rprofile or .Renviron file?

我有办法在.Rprofile或.Renviron文件中设置来自R的包含文件吗?

Thanks

4 个解决方案

#1


5  

You can use the sourceDir function in the Examples section of ?source:

您可以在?source的Examples部分中使用sourceDir函数:

sourceDir <- function(path, trace = TRUE, ...) {
   for (nm in list.files(path, pattern = "\\.[RrSsQq]$")) {
      if(trace) cat(nm,":")           
      source(file.path(path, nm), ...)
      if(trace) cat("\n")
   }
}

And you may want to use sys.source to avoid cluttering your global environment.

您可能希望使用sys.source来避免混乱您的全局环境。

#2


1  

If you set the chdir parameter of source to TRUE, then the source calls within the included file will be relative to its path. Hence, you can call:

如果将source的chdir参数设置为TRUE,则包含文件中的源调用将相对于其路径。因此,您可以致电:

source("~/r_lib/file.R",chdir=T)

It would probably be better not to have source calls within your "library" and make your code into a package, but sometimes this is convenient.

最好不要在“库”中进行源代码调用,并将代码放入包中,但有时这很方便。

#3


0  

Get all the files of your directory, in your case

在您的情况下,获取目录的所有文件

d <- list.files("~/r_lib/")

then you can load them with a function of the plyr package

然后你可以使用plyr包的功能加载它们

library(plyr)  
l_ply(d, function(x) source(paste("~/r_lib/", x, sep = "")))

If you like you can do it in a loop as well or use a different function onstead of l_ply. Conventional loop:

如果你喜欢你也可以在循环中完成它或使用不同的函数而不是l_ply。常规循环:

for (i in 1:length(d)) source(paste("~/r_lib/", d[[i]], sep = ""))

#4


0  

Write your own source() wrapper?

编写自己的source()包装器?

mySource <- function(script, path = "~/r_lib/", ...) {
    ## paste path+filename
    fname <- paste(path, script, sep = "")
    ## source the file
    source(fname, ...)
}

You could stick that in your .Rprofile do is will be loaded each time you start R.

您可以坚持使用.Rprofile do将在每次启动时加载。

If you want to load all the R files, you can extend the above easily to source all files at once

如果要加载所有R文件,可以轻松扩展上述内容以立即获取所有文件

mySource <- function(path = "~/r_lib/", ...) {
    ## list of files
    fnames <- list.files(path, pattern = "\\.[RrSsQq]$")
    ## add path
    fnames <- paste(path, fnames, sep = "")
    ## source the files
    lapply(fnames, source, ...)
    invisible()
}

Actually, though, you'd be better off starting your own private package and loading that.

实际上,你最好开始自己的私人包并加载它。

#1


5  

You can use the sourceDir function in the Examples section of ?source:

您可以在?source的Examples部分中使用sourceDir函数:

sourceDir <- function(path, trace = TRUE, ...) {
   for (nm in list.files(path, pattern = "\\.[RrSsQq]$")) {
      if(trace) cat(nm,":")           
      source(file.path(path, nm), ...)
      if(trace) cat("\n")
   }
}

And you may want to use sys.source to avoid cluttering your global environment.

您可能希望使用sys.source来避免混乱您的全局环境。

#2


1  

If you set the chdir parameter of source to TRUE, then the source calls within the included file will be relative to its path. Hence, you can call:

如果将source的chdir参数设置为TRUE,则包含文件中的源调用将相对于其路径。因此,您可以致电:

source("~/r_lib/file.R",chdir=T)

It would probably be better not to have source calls within your "library" and make your code into a package, but sometimes this is convenient.

最好不要在“库”中进行源代码调用,并将代码放入包中,但有时这很方便。

#3


0  

Get all the files of your directory, in your case

在您的情况下,获取目录的所有文件

d <- list.files("~/r_lib/")

then you can load them with a function of the plyr package

然后你可以使用plyr包的功能加载它们

library(plyr)  
l_ply(d, function(x) source(paste("~/r_lib/", x, sep = "")))

If you like you can do it in a loop as well or use a different function onstead of l_ply. Conventional loop:

如果你喜欢你也可以在循环中完成它或使用不同的函数而不是l_ply。常规循环:

for (i in 1:length(d)) source(paste("~/r_lib/", d[[i]], sep = ""))

#4


0  

Write your own source() wrapper?

编写自己的source()包装器?

mySource <- function(script, path = "~/r_lib/", ...) {
    ## paste path+filename
    fname <- paste(path, script, sep = "")
    ## source the file
    source(fname, ...)
}

You could stick that in your .Rprofile do is will be loaded each time you start R.

您可以坚持使用.Rprofile do将在每次启动时加载。

If you want to load all the R files, you can extend the above easily to source all files at once

如果要加载所有R文件,可以轻松扩展上述内容以立即获取所有文件

mySource <- function(path = "~/r_lib/", ...) {
    ## list of files
    fnames <- list.files(path, pattern = "\\.[RrSsQq]$")
    ## add path
    fnames <- paste(path, fnames, sep = "")
    ## source the files
    lapply(fnames, source, ...)
    invisible()
}

Actually, though, you'd be better off starting your own private package and loading that.

实际上,你最好开始自己的私人包并加载它。