[英]Source multiple r scripts from github
我通常在我的 r 会话中使用一些个人功能,例如:
s1<-"https://github.com/juanchiem/R-sources-Juan/blob/master/rend_aj.R?raw=TRUE" # rend_aj.R
s2<-"https://github.com/juanchiem/R-sources/blob/master/theme_juan.R?raw=TRUE" # theme_juan.R
s3<-"https://github.com/juanchiem/R-sources-Juan/blob/master/assumptions.R?raw=TRUE" # lm_assumptions.R
devtools::source_url(s1)
devtools::source_url(s2)
devtools::source_url(s3)
我想知道是否可以创建一个函数来直接通过文件名从 github 中获取这 3 个文件(在 # 之后详细说明)谢谢!
我们可以编写一个自定义函数(见下面的注释)来实现目标:
custom_source <- function(repo_name,repo_branch,name,
...){
url_to_use <- paste0("https://github.com/",repo_name,
"/blob/",repo_branch,"/",name,".R",
"?raw=TRUE"
)
devtools::source_url(url_to_use,...)
}
调用函数(你可以将它包含在上面的同一个函数中,但我更喜欢单独调用它)
invisible(Map(function(x) custom_source("juanchiem/R-sources-Juan",
"master",x), c("rend_aj","theme_juan", "lm_assumptions")))
结果(截断):
SHA-1 hash of file is 586d156021371098ec51c35c4c056d7c98a94d3d
SHA-1 hash of file is 5c84c1e8e7bd4fe9bf6cd26c4f5e955c20cf851b
SHA-1 hash of file is 63790352a1dc712611bbd26a67cd33d2d1ce5b2c
注意:
.R
文件。 可以根据需要修改。invisible
用于抑制打印到控制台。 但是,添加echo=FALSE
或verbose=FALSE
应该会抑制输出,但事实并非如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.