R/cosore_data.R

##' Load data from the COSORE continuous soil respiration database.
##'
##' The first time this is run for a given version, this function will
##' download the Continuous Soil Respiration (COSORE) from github,
##' using numbered versions), unpack the resulting zip file and load
##' the RData and/or csv files. Subsequent calls will be
##' considerably quicker.
##'
##' The function \code{cosore_delete} deletes all traces of downloaded
##' COSORE data if a version is not given, or a specific version if that
##' is listed.
##' @title Load the COSORE database
##' @param version Version to load.  Verion "1.0.0" corresponds to the
##'   version published in Ecology in 2015.  Other valid versions are
##'   "0.1.0", "0.2.0" and "0.9.0" which are stored on github but are
##'   of historical interest only.
##' @param path Optional path in which to store the data.  If omitted
##'   we use \code{rappdirs} to generate a reasonable path.
##' @export
##' @examples
##' \dontrun{
##' cosore <- cosore_data()
##' head(cosore[[1]]$data)
##' }
cosore_data <- function(version = NULL, path = NULL) {
  datastorr::github_release_get(cosore_data_info(path), version)
}

##' Information to describe how to process github releases
##'
##' @title Github release information
##'
##' @param path Optional path in which to store the data.  If omitted
##'   we use \code{rappdirs} to generate a reasonable path.
cosore_data_info <- function(path = NULL) {
  datastorr::github_release_info(repo = "dfalster/cosore",
                                 filename = "cosore_data.zip",
                                 read = cosore_unpack,
                                 path = path)
}

##' Get release versions
##' @title Get release versions
##' @param local Should we return local (TRUE) or github (FALSE)
##'   version numbers?  Github version numbers are pulled once per
##'   session only.  The exception is for
##'   \code{github_release_version_current} which when given
##'   \code{local = TRUE} will fall back on trying github if there are
##'   no local versions.
##' @param path Optional path in which to store the data.  If omitted
##'   we use \code{rappdirs} to generate a reasonable path.##'
##' @export
cosore_data_versions <- function(local = TRUE, path = NULL) {
  datastorr::github_release_versions(cosore_data_info(path), local)
}

##' @export
##' @rdname cosore_data_versions
cosore_data_version_current <- function(local = TRUE, path = NULL) {
  datastorr::github_release_version_current(cosore_data_info(path), local)
}

##' @export
##' @rdname cosore_data
cosore_data_del <- function(version, path) {
  datastorr::github_release_del(cosore_data_info(path), version)
}

cosore_data_release <- function(description) {
  datastorr::github_release_create(cosore_data_info(path),
                                   description = description, ...)
}

## Given a filename corresponding to a downloaded resource, convert it
## into an R object.
cosore_unpack <- function(filename) {
  dest <- tempfile()
  files <- unzip(filename, exdir = dest)

  ## Some versions have a leading cosore_data, while others don't,
  ## others are badly packaged.  This is terrible, and might get
  ## updated later.
  for (tld in c("cosore_data", "cosore_csv", "cosore")) {
    if (file.exists(file.path(dest, tld))) {
      dest <- file.path(dest, tld)
      break
    }
  }

  csv_files <- dir(dest, pattern = "\\.csv$", full.names = TRUE)
  cosore <- lapply(csv_files, read.csv, stringsAsFactors = FALSE)
  names(cosore) <- sub("cosore_", "",
                     tools::file_path_sans_ext(basename(csv_files)))

  bib_file <- dir(dest, pattern = "\\.bib$", full.names = TRUE)[[1]]
  cosore[["bib"]] <- bibtex::read.bib(bib_file)

  cosore
}
bpbond/cosore.data documentation built on May 28, 2019, 12:32 a.m.