Skip to content

Commit

Permalink
add assert_data_version() #22
Browse files Browse the repository at this point in the history
- add tests for assert_data_version
- add docs for assert_data_version
- run styler
- fix goodpractice warnings
- rebuild docs
  • Loading branch information
gfinak committed Jul 2, 2018
1 parent 774ebb2 commit b6fb48e
Show file tree
Hide file tree
Showing 15 changed files with 628 additions and 475 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(assert_data_version)
export(construct_yml_config)
export(dataVersion)
export(data_version)
Expand Down
13 changes: 6 additions & 7 deletions R/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
#' @importFrom futile.logger flog.debug flog.info flog.warn flog.error flog.fatal flog.appender flog.threshold INFO appender.console appender.tee
#' @importFrom knitr knit
#' @export
#' @examples
#'
#' @examples
#'
#' f <- tempdir()
#' f <- file.path(f,"foo.Rmd")
#' con <- file(f)
#' writeLines("```{r}\n tbl = table(sample(1:10,1000,replace=TRUE)) \n```\n",con=con)
#' close(con)
#' pname <- basename(tempfile())
#' datapackage_skeleton(name=pname,
#' path=tempdir(),
#' path=tempdir(),
#' force = TRUE,
#' r_object_names = "tbl",
#' code_files = f)
#'
#'package_build(file.path(tempdir(),pname))
#'
#' package_build(file.path(tempdir(),pname))
package_build <- function(packageName = NULL,
vignettes = FALSE,
log=INFO) {
Expand Down Expand Up @@ -96,6 +96,5 @@ package_build <- function(packageName = NULL,
#' @rdname keepDataObjects-defunct
#' @export
keepDataObjects <- function(...) {
.Defunct(msg = "keepDataObjects is defunct as of version 0.12.1 of DataPackageR. \nUse the config.yml file to control packaging.")

.Defunct(msg = "keepDataObjects is defunct as of version 0.12.1 of DataPackageR. \nUse the config.yml file to control packaging.") # nolint
}
108 changes: 92 additions & 16 deletions R/dataversion.r
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@



#' Get the DataVersion for a package
#'
#' Retreives the DataVersion of a package if available
Expand All @@ -11,47 +12,42 @@
#' @importFrom utils capture.output file_test package.skeleton packageDescription
#' @export
#' @examples
#'
#'
#' f <- tempdir()
#' f <- file.path(f,"foo.Rmd")
#' con <- file(f)
#' writeLines("```{r}\n tbl = table(sample(1:10,1000,replace=TRUE)) \n```\n",con=con)
#' close(con)
#' pname <- basename(tempfile())
#' datapackage_skeleton(name = pname,
#' path=tempdir(),
#' path=tempdir(),
#' force = TRUE,
#' r_object_names = "tbl",
#' code_files = f)
#'
#' package_build(file.path(tempdir(),pname))
#'
#' devtools::load_all(file.path(tempdir(),pname))
#' data_version(pname)
data_version <- function(pkg, lib.loc = NULL) {
res <- suppressWarnings(
utils::packageDescription(pkg,
lib.loc = lib.loc,
fields = "DataVersion"
)
)
res <- suppressWarnings(utils::packageDescription(pkg,
lib.loc = lib.loc,
fields = "DataVersion"))
if (!is.na(res)) {
package_version(res)
} else {
stop(gettextf(
paste0(
"package %s not found ",
"or has no DataVersion string"
),
paste0("package %s not found ",
"or has no DataVersion string"),
sQuote(pkg)
),
domain = NA
)
domain = NA)
}
}

#' @rdname data_version
#' @export
dataVersion <- function(pkg, lib.loc = NULL){
dataVersion <- function(pkg, lib.loc = NULL) {
warning("Please use data_version() instead of dataVersion().")
data_version(pkg = pkg, lib.loc = lib.loc)
}
Expand All @@ -68,7 +64,8 @@ dataVersion <- function(pkg, lib.loc = NULL){
)
)
}
verstring <- strsplit(pkg_description[["DataVersion"]], "\\.")[[1]]
verstring <-
strsplit(pkg_description[["DataVersion"]], "\\.")[[1]]
names(verstring) <- c("major", "minor", "patch")
verstring[which] <-
as.character(as.numeric(verstring[which]) + 1)
Expand All @@ -77,3 +74,82 @@ dataVersion <- function(pkg, lib.loc = NULL){
new_data_digest[["DataVersion"]] <- verstring
list(pkg_description = pkg_description, new_data_digest = new_data_digest)
}

#' Assert that a data version in a data package matches an expectation.
#'
#' @param data_package_name \code{character} Name of the package.
#' @param version_string \code{character} Version string in "x.y.z" format.
#' @param acceptable \code{character} one of "equal", "equal_or_greater", describing what version match is acceptable.
#' @details Tests the DataVersion string in \code{data_package_name} against \code{version_string} testing the major, minor and revision portion.
#' @return invisible \code{logical} TRUE if success, otherwise stop on mismatch.
#' @details
#' Tests "data_package_name version equal version_string" or "data_package_name version equal_or_greater version_string".
#' @export
#' @examples
#'
#' f <- tempdir()
#' f <- file.path(f, "foo.Rmd")
#' con <- file(f)
#' writeLines("```{r}\n tbl = table(sample(1:10,1000,replace=TRUE)) \n```\n",con = con)
#' close(con)
#' pname <- basename(tempfile())
#' datapackage_skeleton(name = pname,
#' path=tempdir(),
#' force = TRUE,
#' r_object_names = "tbl",
#' code_files = f)
#' package_build(file.path(tempdir(),pname))
#'
#' devtools::load_all(file.path(tempdir(),pname))
#'
#' assert_data_version(data_package_name = pname,version_string = "0.1.0",acceptable = "equal")
assert_data_version <-
function(data_package_name = NULL,
version_string = NULL,
acceptable = "equal") {
acceptable <- match.arg(acceptable, c("equal", "equal_or_greater"))
pkg_version <- data_version(pkg = data_package_name)
required_version <- as.numeric_version(version_string)
base <-
max(10, max(.find_base(pkg_version),
.find_base(required_version))) + 1
if ((acceptable == "equal_or_greater") &
(
.mk_version_numeric(pkg_version, base = base) >=
.mk_version_numeric(required_version, base = base)
)) {
invisible(TRUE)
} else if ((acceptable == "equal") &
(
.mk_version_numeric(pkg_version, base = base) ==
.mk_version_numeric(required_version, base = base)
)) {
invisible(TRUE)
} else {
stop(
paste0(
"Found ",
data_package_name,
" ",
pkg_version,
" but ",
ifelse(acceptable == "equal", " == ", " >= "),
required_version,
" is required."
)
)
}
}

.find_base <- function(v) {
max(as.numeric(v[1, 1]),
as.numeric(v[1, 2]),
as.numeric(v[1, 3]))
}

.mk_version_numeric <- function(x, base = 10) {
as.numeric(x[1, 1]) * base ^ 2 +
as.numeric(x[1, 2]) * base ^ 1 +
as.numeric(x[1, 3]) * base ^
0
}
8 changes: 4 additions & 4 deletions R/environments.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
#' @param name \code{character} the name of the object. Must be a
#' name available in the configuration objects. Other objects are not saved.
#' @details This function is only accessible within an R or Rmd file processed by DataPackageR.
#' It searches for an environment named \code{ENVS} within the current environment,
#' It searches for an environment named \code{ENVS} within the current environment,
#' that holds the object with the given \code{name}. Such an environment is constructed and populated
#' with objects specified in the yaml \code{objects} property and passed along
#' to subsequent R and Rmd files as DataPackageR processes them in order.
#' @return An R object.
#' @export
#' @examples
#' @examples
#'
#' ENVS <- new.env() # ENVS would be in the environment
#' # where the data processing is run. It is
#' # handled automatically by the package.
#' assign("find_me", 100, ENVS) #This is done automatically by DataPackageR
#'
#' datapackager_object_read("find_me") # This would appear in an Rmd processed by
#'
#' datapackager_object_read("find_me") # This would appear in an Rmd processed by
#' # DataPackageR to access the object named "find_me" created
#' # by a previous script. "find_me" would also need to
#' # appear in the objects property of config.yml
Expand Down
56 changes: 28 additions & 28 deletions R/processData.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,76 @@
#'
#' A framework to automate the processing, tidying and packaging of raw data into analysis-ready
#' data sets as R packages.
#'
#' DataPackageR will automate running of data processing code,
#' storing tidied data sets in an R pacakge, producing
#'
#' DataPackageR will automate running of data processing code,
#' storing tidied data sets in an R pacakge, producing
#' data documentation stubs, tracking data object finger prints (md5 hash)
#' and tracking and incrementing a "DataVersion" string
#' in the DESCRIPTION file of the package when raw data or data
#' in the DESCRIPTION file of the package when raw data or data
#' objects change.
#' Code to perform the data processing is passed to DataPacakgeR by the user.
#' The user also specifies the names of the tidy data objects to be stored,
#' documented and tracked in the final package. Raw data should be read from
#' Code to perform the data processing is passed to DataPacakgeR by the user.
#' The user also specifies the names of the tidy data objects to be stored,
#' documented and tracked in the final package. Raw data should be read from
#' "inst/extdata" but large raw data files can be read from sources external
#' to the package source tree.
#'
#'
#' Configuration is controlled via the config.yml file created at the package root.
#' Its properties include a list of R and Rmd files that are to be rendered / sourced and
#' which read data and do the actual processing.
#' It also includes a list of r object names created by those files. These objects
#' are stored in the final package and accessible via the \code{data()} API.
#' The documentation for these objects is accessible via "?object-name", and md5
#' fingerprints of these objects are created and tracked.
#' The documentation for these objects is accessible via "?object-name", and md5
#' fingerprints of these objects are created and tracked.
#'
#' The Rmd and R files used to process the objects are transformed into vignettes
#' accessible in the final package so that the processing is fully documented.
#'
#' A DATADIGEST file in the package source keeps track of the data object fingerprints.
#' A DataVersion string is added to the package DESCRIPTION file and upated when these
#'
#' A DATADIGEST file in the package source keeps track of the data object fingerprints.
#' A DataVersion string is added to the package DESCRIPTION file and upated when these
#' objects are updated or changed on subsequent builds.
#'
#'
#' Once the package is built and installed, the data objects created in the package are accessible via
#' the \code{data()} API, and
#' Calling \code{datapackage_skeleton()} and passing in R / Rmd file names, and r object names
#' the \code{data()} API, and
#' Calling \code{datapackage_skeleton()} and passing in R / Rmd file names, and r object names
#' onstructs a skeleton data package source tree and an associated \code{config.yml} file.
#'
#'
#' Calling \code{build_package()} sets the build process in motion.
#' @examples
#' @examples
#' # A simple Rmd file that creates one data object
#' # named "tbl".
#' f <- tempdir()
#' f <- file.path(f,"foo.Rmd")
#' con <- file(f)
#' writeLines("```{r}\n tbl = table(sample(1:10,1000,replace=TRUE)) \n```\n",con=con)
#' close(con)
#'
#'
#' # construct a data package skeleton named "MyDataPackage" and pass
#' # in the Rmd file name with full path, and the name of the object(s) it
#' # creates.
#'
#'
#' pname <- basename(tempfile())
#' datapackage_skeleton(name=pname,
#' path=tempdir(),
#' path=tempdir(),
#' force = TRUE,
#' r_object_names = "tbl",
#' code_files = f)
#'
#' # call build_package to run the "foo.Rmd" processing and
#'
#' # call build_package to run the "foo.Rmd" processing and
#' # build a data package.
#' package_build(file.path(tempdir(), pname))
#'
#'
#' # "install" the data package
#' devtools::load_all(file.path(tempdir(), pname))
#'
#'
#' # read the data version
#' data_version(pname)
#'
#'
#' # list the data sets in the package.
#' data(package = pname)
#'
#'
#' # The data objects are in the package source under "/data"
#' list.files(pattern="rda", path = file.path(tempdir(),pname,"data"), full = TRUE)
#'
#'
#' # The documentation that needs to be edited is in "/R"
#' list.files(pattern="R", path = file.path(tempdir(), pname,"R"), full = TRUE)
#' readLines(list.files(pattern="R", path = file.path(tempdir(),pname,"R"), full = TRUE))
Expand Down
20 changes: 11 additions & 9 deletions R/skeleton.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ datapackage_skeleton <-
#' close(con)
#' pname <- basename(tempfile())
#' datapackage_skeleton(name = pname,
#' path = tempdir(),
#' path = tempdir(),
#' force = TRUE,
#' r_object_names = "tbl",
#' code_files = f)
Expand All @@ -178,11 +178,13 @@ datapackage.skeleton <- function(name = NULL,
code_files = character(),
r_object_names = character()) {
warning("Please use datapackage_skeleton() instead of datapackage.skeleton()")
datapackage_skeleton(name = name,
list = list,
environment = environment,
path = path,
force = force,
code_files = code_files,
r_object_names = r_object_names)
}
datapackage_skeleton(
name = name,
list = list,
environment = environment,
path = path,
force = force,
code_files = code_files,
r_object_names = r_object_names
)
}
2 changes: 1 addition & 1 deletion R/yamlR.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' close(con)
#' pname <- basename(tempfile())
#' datapackage_skeleton(name=pname,
#' path = tempdir(),
#' path = tempdir(),
#' force = TRUE,
#' r_object_names = "tbl",
#' code_files = f)
Expand Down

0 comments on commit b6fb48e

Please sign in to comment.