Skip to content

Commit

Permalink
Fix #34
Browse files Browse the repository at this point in the history
Add tests.
Add options to datapackage_skeleton to move data and dependencies into location in the package.
  • Loading branch information
gfinak committed Jul 31, 2018
1 parent 1825fad commit f37b3aa
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 17 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -22,7 +22,8 @@ Imports:
stringr,
futile.logger,
rprojroot,
usethis
usethis,
crayon
VignetteBuilder: knitr
RoxygenNote: 6.1.0
Encoding: UTF-8
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -23,6 +23,8 @@ export(yml_remove_files)
export(yml_remove_objects)
export(yml_write)
importFrom(assertthat,assert_that)
importFrom(crayon,bold)
importFrom(crayon,green)
importFrom(desc,desc)
importFrom(devtools,build)
importFrom(devtools,build_vignettes)
Expand Down
21 changes: 14 additions & 7 deletions R/processData.R
Expand Up @@ -271,7 +271,8 @@ DataPackageR <- function(arg = NULL, deps = TRUE) {
knitr::spin(r_files[i], precious = TRUE,
knit = FALSE)
r_files[i] <- paste0(tools::file_path_sans_ext(r_files[i]),".Rmd")
assert_that(file.exists(r_files[i]), msg = paste0("File: ",r_files[i]," does not exist!"))
assert_that(file.exists(r_files[i]),
msg = paste0("File: ",r_files[i]," does not exist!"))
lines <- readLines(r_files[i])
lines <- c("---",
paste0("title: ",basename(r_files[i])),
Expand Down Expand Up @@ -631,10 +632,13 @@ project_path <- function(file = NULL){
#' @examples
#' project_extdata_path(file = "mydata.csv")
project_extdata_path <- function( file = NULL ){
if (is.null(file))
if (is.null(file)){
return(file.path(usethis::proj_get(),"inst","extdata"))
else
return(normalizePath(file.path(usethis::proj_get(),"inst","extdata",file),winslash = "/"))
} else {
return(normalizePath(
file.path(usethis::proj_get(),
"inst","extdata",file),winslash = "/"))
}
}

#' Get DataPackageR data path
Expand All @@ -649,8 +653,11 @@ project_extdata_path <- function( file = NULL ){
#' @examples
#' project_data_path( file = "data.rda" )
project_data_path <- function( file = NULL ){
if (is.null(file))
if (is.null(file)) {
return(file.path(usethis::proj_get(),"data"))
else
return(normalizePath(file.path(usethis::proj_get(),"data",file),winslash = "/"))
} else {
return(normalizePath(
file.path(usethis::proj_get(),
"data",file),winslash = "/"))
}
}
37 changes: 29 additions & 8 deletions R/skeleton.R
Expand Up @@ -26,14 +26,19 @@
#' @param code_files Optional \code{character} vector of paths to Rmd files that process raw data
#' into R objects.
#' @param r_object_names \code{vector} of quoted r object names , tables, etc. created when the files in \code{code_files} are run.
#' @param raw_data_dir \code{character} pointing to a raw data directory. Will be moved with all its subdirectories to "inst/extdata"
#' @param dependencies \code{vector} of \code{character}, paths to R files that will be moved to "data-raw" but not included in the yaml config file. e.g., dependency scripts.
#' @note renamed \code{datapackage.skeleton()} to \code{datapackage_skeleton()}.
#' @importFrom crayon bold green
#' @export
datapackage_skeleton <-
function(name = NULL,
path = ".",
force = FALSE,
code_files = character(),
r_object_names = character()) {
r_object_names = character(),
raw_data_dir = character(),
dependencies = character()) {
if (is.null(name)) {
stop("Must supply a package name", call. = FALSE)
}
Expand Down Expand Up @@ -90,14 +95,30 @@ datapackage_skeleton <-
message("configuring yaml file")
# Rather than copy, read in, modify (as needed), and write.
# process the string
if (length(code_files) != 0) {
.codefile_validate(code_files)
# copy them over
purrr::map(code_files, function(x)
file.copy(x, file.path(package_path, "data-raw"), overwrite = TRUE))
.copy_files_to_data_raw <- function(x, obj = c("code","dependencies")){
if (length(x) != 0) {
.codefile_validate(x)
# copy them over
obj = match.arg(obj, c("code","dependencies"))
message(paste0(crayon::bold("Moving ",obj," into "), crayon::green("data-raw")))
# purrr::map(x, function(y)
for (y in x)
file.copy(y, file.path(package_path, "data-raw"), overwrite = TRUE)
}
}



.copy_data_to_inst_extdata <- function(x){
if (length(x) != 0) {
# copy them over
cat(crayon::bold("Moving raw data into"), crayon::green("inst/extdata"))
file.copy(x, file.path(package_path, "inst/extdata"),
recursive = TRUE, overwrite = TRUE)
}
}
.copy_files_to_data_raw(code_files, obj = "code")
.copy_files_to_data_raw(dependencies, obj = "dependencies")
.copy_data_to_inst_extdata(raw_data_dir)

yml <- construct_yml_config(code = code_files, data = r_object_names)
yaml::write_yaml(yml, file = file.path(package_path, "datapackager.yml"))
} else {
Expand Down
4 changes: 4 additions & 0 deletions inst/extdata/tests/raw_data/testdata.csv
@@ -0,0 +1,4 @@
1, name,value
2, Red, 50
3, Blue, 200
4, Green, 10
7 changes: 6 additions & 1 deletion man/datapackage_skeleton.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions tests/testthat/test-skeleton-data-dependencies.R
@@ -0,0 +1,58 @@
context("skeleton")
test_that("data, code, and dependencies are moved into place by skeleton", {
file <- system.file("extdata", "tests", "extra.rmd",
package = "DataPackageR")
ancillary <- system.file("extdata","tests","rfileTest.R",
package = "DataPackageR")
raw_data <- system.file("extdata", "tests", "raw_data",
package = "DataPackageR")
expect_null(
datapackage_skeleton(
name = "datatest",
path = tempdir(),
code_files = c(file),
force = TRUE,
r_object_names = "data",
raw_data_dir = raw_data,
dependencies = ancillary
)
)
expect_true(
file.exists(
normalizePath(
file.path(
tempdir(),
"datatest",
"inst",
"extdata",
"raw_data",
"testdata.csv"),
winslash = "/")
)
)
expect_true(
file.exists(
normalizePath(
file.path(
tempdir(),
"datatest",
"data-raw",
"extra.rmd"),
winslash = "/")
)
)
expect_true(
file.exists(
normalizePath(
file.path(
tempdir(),
"datatest",
"data-raw",
"rfileTest.R"),
winslash = "/")
)
)
unlink(file.path(tempdir(), "datatest"),
recursive = TRUE,
force = TRUE)
})

0 comments on commit f37b3aa

Please sign in to comment.