您可以使用R-Bloggers上提供的解决方案:
source_github <- function(u) {
# load package
require(RCurl)
# read script lines from website
script <- getURL(u, ssl.verifypeer = FALSE)
# parase lines and evaluate in the global environment
eval(parse(text = script))
}
source("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R")
Run Code Online (Sandbox Code Playgroud)
对于要在全局环境中评估的函数(我猜你更喜欢这个解决方案),你可以使用:
source_https <- function(u, unlink.tmp.certs = FALSE) {
# load package
require(RCurl)
# read script lines from website using a security certificate
if(!file.exists("cacert.pem")) download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile = "cacert.pem")
script <- getURL(u, followlocation = TRUE, cainfo = "cacert.pem")
if(unlink.tmp.certs) unlink("cacert.pem")
# parase lines and evealuate in the global environement
eval(parse(text = script), envir= .GlobalEnv)
}
source_https("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R")
source_https("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/htmlToText/htmlToText.R", unlink.tmp.certs = TRUE)
Run Code Online (Sandbox Code Playgroud)
正如提到的原文章由托尼Breyal,上所以这个讨论也应记,因为它是相关的讨论问题.
小智 6
基于@Matifou 的回复,但使用附加?raw=TRUE在 URL 末尾的“新”方法:
devtools::source_url("https://github.com/tonybreyal/Blog-Reference-Functions/blob/master/R/bingSearchXScraper/bingSearchXScraper.R?raw=TRUE")
Run Code Online (Sandbox Code Playgroud)
如果它是 GitHub 上的链接,您可以点击Raw旁边的链接Blame,那么实际上您可以使用普通的base::source. 转到您选择的 R 脚本并找到Raw按钮。
该链接现在将包含raw.githubusercontent.com,并且该页面仅显示 R 脚本本身。然后,对于这个例子,
source(
paste0(
"https://raw.githubusercontent.com/betanalpha/knitr_case_studies/master/",
"stan_intro/stan_utility.R"
)
)
Run Code Online (Sandbox Code Playgroud)
(paste0只是为了让 URL 适合更窄的屏幕而使用。)
你可以简单地使用 SOURCE_URL从包devtools:
library(devtools)
source_url("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R")
Run Code Online (Sandbox Code Playgroud)