如何从 Rstudio 的 github 等在线 cpp 开源资源中获取资源?

How to source from online cpp open source like github from Rstudio?

我正在尝试通过以下方式获取在线存储库中存在的源代码:

Rcpp::sourceCpp(
 url("https://github.com/slwu89/MCMC/blob/master/adaptMCMC_source.cpp")
)

我遇到了这个问题:

Error in dirname(file) : a character vector argument expected

只需使用 R 的 download.file():

library(Rcpp)
remurl <- "https://github.com/slwu89/MCMC/blob/master/adaptMCMC_source.cpp"
locfile <- "/tmp/mcmc.cpp"
download.file(url=remurl, destfile=locfile)
sourceCpp(locfile)   # dozens of error for _this_ file

编辑 这里有一个更好的方法 两个 重要修复:

  1. 您需要一个不同的 URL。您列出的那个将下载 html 页面。但是您需要原始源代码,在本例中为 https://raw.githubusercontent.com/slwu89/MCMC/master/adaptMCMC_source.cpp
  2. 你可以创建一个简单的辅助函数,它接受 url,创建一个扩展名为 .cpp 的临时文件(嘿,这个参数曾经是我对 base R 的补丁 ;-) 然后 returns那个文件名。

见下文:

u2f <- function(url) { 
   tf <- tempfile() 
   download.file(url, tf, quiet=TRUE) 
   tf 
}
library(Rcpp)
url <- "https://raw.githubusercontent.com/slwu89/MCMC/master/adaptMCMC_source.cpp"
sourceCpp( u2f( url ) )

编译正常(尽管有关于 signed/unsigned 比较的警告)。