Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: prioritize targets over drake #63

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Suggests:
kableExtra,
rcoder,
labelled,
targets,
drake (>= 7.11.0),
panelcleaner,
kfa,
callr,
Expand All @@ -43,9 +43,9 @@ BugReports: https://github.com/nyuglobalties/blueprintr/issues
Depends:
R (>= 3.5.0)
Imports:
targets,
rlang,
here,
drake (>= 7.11.0),
glue,
magrittr,
readr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export(read_metadata)
export(render_codebook)
export(render_kfa_report)
export(tar_blueprint)
export(tar_blueprint_raw)
export(tar_blueprints)
export(vis_table_lineage)
export(vis_variable_lineage)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# blueprintr 0.2.5.9000 (dev version)
* Switch default workflow executor to targets
* Update vignettes to demonstrate current workflow procedure (@hgao1, #59)

# blueprintr 0.2.5
Expand Down
2 changes: 1 addition & 1 deletion R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ all_types_match <- function(df, meta) {
df_types %>%
tidytable::left_join(
meta %>%
tidytable::select(.data$name, expected_type = .data$type),
tidytable::select("name", expected_type = "type"),
by = "name"
) %>%
tidytable::mutate(issue = .data$expected_type != .data$type)
Expand Down
8 changes: 4 additions & 4 deletions R/metadata-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ reconcile_dependencies <- function(dec_dt, meta_dt) {

wide_meta <- tidytable::pivot_wider(
all_meta,
names_from = .data$.origin,
names_from = ".origin",
values_from = tidytable::all_of(fields)
)

Expand All @@ -141,7 +141,7 @@ reconcile_dependencies <- function(dec_dt, meta_dt) {

wide_meta <- tidytable::rename(
wide_meta,
type = .data$type_annotations
type = "type_annotations"
)

wide_meta[["type_metafile"]] <- NULL
Expand All @@ -159,8 +159,8 @@ reconcile_dependencies <- function(dec_dt, meta_dt) {

wide_meta <- tidytable::relocate(
wide_meta,
.data[[field]],
.before = .data[[decs]]
tidyselect::all_of(field),
.before = tidyselect::all_of(decs)
)

wide_meta[[decs]] <- NULL
Expand Down
17 changes: 17 additions & 0 deletions R/plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#' @return A drake plan with all of the necessary blueprint steps
#' @export
plan_from_blueprint <- function(blueprint) {
check_drake_installed()

plan <- drake::drake_plan()

# drake_plan() does not add the class by default
Expand All @@ -17,6 +19,21 @@ plan_from_blueprint <- function(blueprint) {
attach_blueprint(plan, blueprint)
}

check_drake_installed <- function() {
if (!requireNamespace("drake", quietly = TRUE)) {
drake_not_installed_error()
}
}

drake_not_installed_error <- function() {
bp_err(c(
"As of blueprintr 0.3.0, targets is the preferred execution engine ",
"and is thus installed automatically.\n",
"To use the drake functionality (anything with \"plans\"), ",
"please run `install.packages('drake')`."
))
}

#' @rdname attach_blueprint
#' @export
attach_blueprints <- function(plan, ...) {
Expand Down
3 changes: 3 additions & 0 deletions R/tar_blueprint.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @param directory A folder containing R scripts that evaluate to `blueprint()`
#' objects
#' @param recurse Recursively loads blueprints from a directory if `TRUE`
#' @param bp A blueprint object
#' @return A `list()` of `tar_target` objects

#' @details # Empty blueprint folder
Expand Down Expand Up @@ -40,6 +41,8 @@ tar_blueprints <- function(directory = here::here("blueprints"),
flatten(raw_targets)
}

#' @rdname tar_blueprint
#' @export
tar_blueprint_raw <- function(bp) {
# Suppress loading objects if in interactive macro eval mode
old_state <- options(blueprintr.attach_state = TRUE)
Expand Down
4 changes: 2 additions & 2 deletions inst/blueprints/student_demographics.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name,type,description
student_id,character,Student ID
age,double,NA
grade,double,NA
classroom_id,character,Classroom ID
grade,double,NA
student_id,character,Student ID
2 changes: 1 addition & 1 deletion inst/codebook_templates/default_codebook.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ params:

```{r startup_checks, include=FALSE}
stopifnot(is.data.frame(params$dataset) || is.null(params$dataset))
stopifnot(inherits(params$blueprint, "blueprint"))
stopifnot(inherits(params$blueprint, "blueprint"))
stopifnot(inherits(params$meta, "blueprint_metadata"))
```

Expand Down
2 changes: 1 addition & 1 deletion man/blueprintr-package.Rd

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

5 changes: 5 additions & 0 deletions man/tar_blueprint.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/helper-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,25 @@ expect_equiv <- function(object, expected, ...) {
attributes(expected) <- NULL
expect_equal(object, expected, ...)
}

mock_drake_plan <- function(...) {
dots <- match.call(expand.dots = FALSE)$...

out <- data.frame(
target = names(dots)
)
out$command <- dots

structure(
out,
class = c("drake_plan", class(out))
)
}

tar_make_local <- function(..., envir = parent.frame()) {
targets::tar_make(..., callr_function = NULL, envir = envir)
}

tar_manifest_local <- function(..., envir = parent.frame()) {
targets::tar_manifest(..., callr_function = NULL, envir = envir)
}
14 changes: 7 additions & 7 deletions tests/testthat/test-01-blueprint-ast.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ test_that("Chunk comprehension works", {
.TARGET("chunk1") %>%
left_join(
.TARGET("chunk2") %>%
select(site_id, tent),
select(site_id, tent),
by = "site_id"
) %>%
mutate(still_tent = tent_last == tent)
})

answer <- bquote({
chunk1 %>%
left_join(
chunk2 %>%
select(site_id, tent),
chunk1 %>%
left_join(
chunk2 %>%
select(site_id, tent),
by = "site_id"
) %>%
mutate(still_tent = tent_last == tent)
Expand Down Expand Up @@ -70,15 +70,15 @@ test_that("Chunk comprehension works", {

command_4 <- quote(
.TARGET("dataset") %>%
processing$process(param = TRUE) %>%
processing$process(param = TRUE) %>%
mutate(
var = processing$func(variable)
)
)

answer_4 <- quote(
dataset %>%
processing$process(param = TRUE) %>%
processing$process(param = TRUE) %>%
mutate(
var = processing$func(variable)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-01-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ test_that("Coalescing annotation vs. metafile deps works", {

reconciled <- reconcile_dependencies(linked, meta_dt_bad)

# Demontrates that the actual variables found in the
# Demonstrates that the actual variables found in the
# dataset are preserved
expect_true(
setequal(reconciled$name, c("x", "y"))
)

# Demontrates that the actual type information of
# Demonstrates that the actual type information of
# the underlying is preserved
expect_identical(
reconciled$type[[1]],
Expand Down
107 changes: 61 additions & 46 deletions tests/testthat/test-02-blueprints.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
context("blueprints")

test_that("blueprint tests are run", {
mtcars_bp <- blueprint(
"mtcars_chunk",
command = {
mtcars
},
metadata_directory = bp_path("blueprints")
)

plan <- plan_from_blueprint(mtcars_bp)

drake::clean()
expect_message(drake::make(plan))

bad_mtcars_bp <- blueprint(
"bad_mtcars_chunk",
command = {
df <- .TARGET("mtcars_chunk")
df$mpg <- NULL
df
},
metadata_file_path =
file.path(
bp_path("blueprints"),
"mtcars_chunk.csv"
)
)
targets::tar_dir({
mtcars_bp <- blueprint(
"mtcars_chunk",
command = {
mtcars
},
metadata_directory = bp_path("blueprints")
)

targets::tar_script({
blueprintr::tar_blueprint_raw(mtcars_bp)
})

expect_message(tar_make_local())

bad_mtcars_bp <- blueprint(
"bad_mtcars_chunk",
command = {
df <- .TARGET("mtcars_chunk")
df$mpg <- NULL
df
},
metadata_file_path =
file.path(
bp_path("blueprints"),
"mtcars_chunk.csv"
)
)

plan <- attach_blueprints(plan, bad_mtcars_bp)
targets::tar_script(
{
list(
blueprintr::tar_blueprint_raw(mtcars_bp),
blueprintr::tar_blueprint_raw(bad_mtcars_bp)
)
},
ask = FALSE
)

drake::clean()
expect_error(drake::make(plan))
expect_error(tar_make_local())
})
})

test_that("Dependencies are handled properly", {
Expand Down Expand Up @@ -77,21 +87,26 @@ test_that("Dependencies are handled properly", {
expect_identical(blueprint_target_deps(id_bp), character())
expect_identical(blueprint_target_deps(student_demo_bp), "id_vars")

plan <- plan_from_blueprint(id_bp) %>%
attach_blueprint(student_demo_bp)

drake::clean()
drake::make(plan)

student_demo_meta <- drake::readd(student_demographics_meta)
id_vars_meta <- drake::readd(id_vars_meta)

expect_identical(
student_demo_meta %>%
tidytable::filter(.data$name == "student_id") %>%
tidytable::pull(.data$description),
id_vars_meta %>%
tidytable::filter(.data$name == "student_id") %>%
tidytable::pull(.data$description)
)
targets::tar_dir({
targets::tar_script({
list(
blueprintr::tar_blueprint_raw(id_bp),
blueprintr::tar_blueprint_raw(student_demo_bp)
)
})

tar_make_local()

student_demo_meta <- targets::tar_read(student_demographics_meta)
id_vars_meta <- targets::tar_read(id_vars_meta)

expect_identical(
student_demo_meta %>%
tidytable::filter(.data$name == "student_id") %>%
tidytable::pull(.data$description),
id_vars_meta %>%
tidytable::filter(.data$name == "student_id") %>%
tidytable::pull(.data$description)
)
})
})
Loading
Loading