![](/img/trans.png)
[英]How to extract one or more words from a string and search for them in two different columns form another file in R
[英]Define all functions in one .R file, call them from another .R file. How, if possible?
如何在另一个文件中调用abc.R文件中定义的函数,比如xyz.R?
补充问题是,如何从R提示符/命令行调用abc.R中定义的函数?
你可以调用source("abc.R")
其次是source("xyz.R")
假定这两个文件都在你的当前工作目录。
如果abc.R是:
fooABC <- function(x) {
k <- x+1
return(k)
}
和xyz.R是:
fooXYZ <- function(x) {
k <- fooABC(x)+1
return(k)
}
然后这将工作:
> source("abc.R")
> source("xyz.R")
> fooXYZ(3)
[1] 5
>
即使存在周期性依赖关系,这也会有效。
例如,如果abc.R是这样的:
fooABC <- function(x) {
k <- barXYZ(x)+1
return(k)
}
barABC <- function(x){
k <- x+30
return(k)
}
和xyz.R是这样的:
fooXYZ <- function(x) {
k <- fooABC(x)+1
return(k)
}
barXYZ <- function(x){
k <- barABC(x)+20
return(k)
}
然后,
> source("abc.R")
> source("xyz.R")
> fooXYZ(3)
[1] 55
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.