是否有人知道在安装过程中从Internet下载数据集的软件包,然后准备并保存它以便在使用时加载软件包时可用library(packageName)?这种方法是否有任何缺点(除了明显的一个,如果数据源不可用或数据格式发生变化,软件包安装将失败)?
编辑:一些背景.数据是ZIP存档中的三个以制表符分隔的文件,由联邦统计数据拥有,通常可以自由访问.我有R代码下载,提取和准备数据,最后创建三个数据帧,可以.RData格式保存.
我正在考虑创建两个包:一个提供数据的"数据"包,以及一个对其进行操作的"代码"包.
当你发布编辑时,我之前做过这个模型。我认为它会起作用,但未经测试。我已经对其进行了评论,以便您可以看到需要更改的内容。这里的想法是检查当前工作环境中是否存在预期的对象。如果不是,请检查可在其中找到数据的文件是否位于当前工作目录中。如果未找到,则提示用户下载该文件,然后从那里继续。
myFunction <- function(this, that, dataset) {
# We're giving the user a chance to specify the dataset.
# Maybe they have already downloaded it and saved it.
if (is.null(dataset)) {
# Check to see if the object is already in the workspace.
# If it is not, check to see whether the .RData file that
# contains the object is in the current working directory.
if (!exists("OBJECTNAME", where = 1)) {
if (isTRUE(list.files(
pattern = "^DATAFILE.RData$") == "DATAFILE.RData")) {
load("DATAFILE.RData")
# If neither of those are successful, prompt the user
# to download the dataset.
} else {
ans = readline(
"DATAFILE.RData dataset not found in working directory.
OBJECTNAME object not found in workspace. \n
Download and load the dataset now? (y/n) ")
if (ans != "y")
return(invisible())
# I usually use RCurl in case the URL is https
require(RCurl)
baseURL = c("http://some/base/url/")
# Here, we actually download the data
temp = getBinaryURL(paste0(baseURL, "DATAFILE.RData"))
# Here we load the data
load(rawConnection(temp), envir=.GlobalEnv)
message("OBJECTNAME data downloaded from \n",
paste0(baseURL, "DATAFILE.RData \n"),
"and added to your workspace\n\n")
rm(temp, baseURL)
}
}
dataset <- OBJECTNAME
}
TEMP <- dataset
## Other fun stuff with TEMP, this, and that.
}
Run Code Online (Sandbox Code Playgroud)
这是另一种方法,基于 @juba 和我之间的评论。基本概念是,正如您所描述的,一个用于代码的包和一个用于数据的包。该函数将是包含您的代码的包的一部分。它会:
当任何检查失败时,它会询问用户是否要更新软件包的安装。在本例中,为了进行演示,我链接到了 Github 上正在开发的一个包。这应该会让您了解在将其托管到您自己的包后需要替换什么才能使其与您自己的包一起工作。
CheckVersionFirst <- function() {
# Check to see if installed
if (!"StataDCTutils" %in% installed.packages()[, 1]) {
Checks <- "Failed"
} else {
# Compare version numbers
require(RCurl)
temp <- getURL("https://raw.github.com/mrdwab/StataDCTutils/master/DESCRIPTION")
CurrentVersion <- gsub("^\\s|\\s$", "",
gsub(".*Version:(.*)\\nDate.*", "\\1", temp))
if (packageVersion("StataDCTutils") == CurrentVersion) {
Checks <- "Passed"
}
if (packageVersion("StataDCTutils") < CurrentVersion) {
Checks <- "Failed"
}
}
switch(
Checks,
Passed = { message("Everything looks OK! Proceeding!") },
Failed = {
ans = readline(
"'StataDCTutils is either outdated or not installed. Update now? (y/n) ")
if (ans != "y")
return(invisible())
require(devtools)
install_github("StataDCTutils", "mrdwab")
})
# Some cool things you want to do after you are sure the data is there
}
Run Code Online (Sandbox Code Playgroud)
尝试一下CheckVersionFirst()。
注意:只有当您每次将新版本的数据推送到 Github 时都认真记住更新描述文件中的版本号时,此操作才会成功!
因此,为了澄清/回顾/扩展,基本思想是:
DESCRIPTIONCheckVersionFirst()函数作为.onLoad事件集成到您的代码包中。(显然修改函数以匹配您的帐户和包名称)。# Some cool things you want to do after you are sure the data is there以反映您真正想做的很酷的事情,这可能会从library(YOURDATAPACKAGE)加载数据开始......