gpt4 book ai didi

r - 在安装过程中从互联网下载数据的包

转载 作者:行者123 更新时间:2023-12-04 12:33:39 24 4
gpt4 key购买 nike

是否有人知道在安装过程中从 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.
}

两个包,托管在 Github

这是另一种方法,基于@juba 和我之间的评论。基本概念是,如您所描述的,拥有一个用于代码的包和一个用于数据的包。此函数将是包含您的代码的包的一部分。它会:
  • 查看数据包是否安装
  • 检查您安装的数据包的版本是否与 Github 上的版本匹配,我们将假设它是最新版本。

  • 当它没有通过任何检查时,它会询问用户是否要更新他们的软件包安装。在这种情况下,为了演示,我已经链接到我在 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
    }

    CheckVersionFirst() 试试看.

    Note: This would succeed only if you religiously remember to update your version number in your description file every time you push a new version of the data to Github!



    因此,为了澄清/回顾/扩展,基本思想是:
  • 定期推送你的的更新版本数据打包到Github,请务必更改的版本号数据包装在其 DESCRIPTION文件时这样做。
  • 集成此 CheckVersionFirst()用作 .onLoad您的事件 代码包裹。 (显然修改函数以匹配您的帐户和包名称)。
  • 更改注释行 # Some cool things you want to do after you are sure the data is there反射(reflect)你真正想做的很酷的事情,这可能以 library(YOURDATAPACKAGE) 开头加载数据....
  • 关于r - 在安装过程中从互联网下载数据的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14871818/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com