[英]Define all functions in one .R file, call them from another .R file. How, if possible?
How do I call functions defined in abc.R file in another file, say xyz.R? 如何在另一个文件中调用abc.R文件中定义的函数,比如xyz.R?
A supplementary question is, how do I call functions defined in abc.R from the R prompt/command line? 补充问题是,如何从R提示符/命令行调用abc.R中定义的函数?
You can call source("abc.R")
followed by source("xyz.R")
(assuming that both these files are in your current working directory. 你可以调用source("abc.R")
其次是source("xyz.R")
假定这两个文件都在你的当前工作目录。
If abc.R is: 如果abc.R是:
fooABC <- function(x) {
k <- x+1
return(k)
}
and xyz.R is: 和xyz.R是:
fooXYZ <- function(x) {
k <- fooABC(x)+1
return(k)
}
then this will work: 然后这将工作:
> source("abc.R")
> source("xyz.R")
> fooXYZ(3)
[1] 5
>
Even if there are cyclical dependencies, this will work. 即使存在周期性依赖关系,这也会有效。
Eg If abc.R is this: 例如,如果abc.R是这样的:
fooABC <- function(x) {
k <- barXYZ(x)+1
return(k)
}
barABC <- function(x){
k <- x+30
return(k)
}
and xyz.R is this: 和xyz.R是这样的:
fooXYZ <- function(x) {
k <- fooABC(x)+1
return(k)
}
barXYZ <- function(x){
k <- barABC(x)+20
return(k)
}
then, 然后,
> source("abc.R")
> source("xyz.R")
> fooXYZ(3)
[1] 55
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.