r - 从 github 获取多个 r 脚本
问题描述
我通常在 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
orverbose=FALSE
应该已经抑制了输出,但它没有。