Skip to content

Commit

Permalink
Merge branch 'release/3.11.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
psychelzh committed Apr 6, 2024
2 parents 0e5d9d4 + fd41e33 commit 255fc81
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 88 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tarflow.iquizoo
Title: Setup "targets" Workflows for "iquizoo" Data Processing
Version: 3.11.0
Version: 3.11.1
Authors@R: c(
person("Liang", "Zhang", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9041-1150")),
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export(setup_option_file)
export(setup_source)
export(setup_templates)
export(tar_fetch_data)
export(tar_fetch_users)
export(tar_prep_hash)
export(tar_prep_iquizoo)
export(tar_prep_proj)
export(tar_prep_raw)
export(use_targets_pipeline)
import(rlang)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# tarflow.iquizoo 3.11.1

* Separate `tar_prep_proj()` into `tar_prep_hash()` and `tar_fetch_users()`.

# tarflow.iquizoo 3.11.0

## Breaking Changes
Expand Down
9 changes: 9 additions & 0 deletions R/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,12 @@ default_file <- function() {
}

# nocov end

check_templates <- function(templates) {
if (!inherits(templates, "tarflow.template")) {
cli::cli_abort(
"{.arg templates} must be created by {.fun setup_templates}.",
class = "tarflow_bad_templates"
)
}
}
107 changes: 57 additions & 50 deletions R/targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ tar_prep_iquizoo <- function(params, ...,
templates = setup_templates(),
check_progress = TRUE) {
check_dots_empty()
if (!inherits(templates, "tarflow.template")) {
cli::cli_abort(
"{.arg templates} must be created by {.fun setup_templates}.",
class = "tarflow_bad_templates"
)
}
check_templates(templates)
what <- match.arg(what, several.ok = TRUE)
action_raw_data <- match.arg(action_raw_data)
if (!is.null(combine) && !all(combine %in% objects())) {
Expand Down Expand Up @@ -78,10 +73,14 @@ tar_prep_iquizoo <- function(params, ...,
"contents_origin",
expr(unserialize(!!serialize(contents, NULL)))
),
tar_prep_proj(contents, templates, check_progress),
if (check_progress) tar_prep_hash(contents, templates),
tar_fetch_users(contents, templates),
sapply(
what,
\(what) tar_fetch_data(contents, templates, what, check_progress),
tar_fetch_data,
contents = contents,
templates = templates,
check_progress = check_progress,
simplify = FALSE
),
if ("raw_data" %in% what && action_raw_data != "none") {
Expand All @@ -105,54 +104,61 @@ tar_prep_iquizoo <- function(params, ...,
)
}

#' Generate a set of targets for preparing project-level data
#' Generate a set of targets for fetching progress hash
#'
#' The progress hash stores the progress of the project, which is used to check
#' whether the project is updated.
#'
#' There are mainly two types of data to be fetched, i.e., the progress hash and
#' the user information. The former is used to check the progress of the
#' project, while the latter is used to identify the users involved in the
#' project.
#' These objects are named as `progress_hash_{project_id}` for each project.
#'
#' @param contents The contents structure used as the configuration of data
#' fetching.
#' @param templates The SQL template files used to fetch data. See
#' [setup_templates()] for details.
#' @param check_progress Whether to check the progress hash. When set as `TRUE`,
#' a progress hash objects named as `progress_hash_{project_id}` for each
#' project will be added into the target list. Set it as `FALSE` if the
#' projects are finalized.
#' @return A list of target objects.
#' @export
tar_prep_proj <- function(contents,
templates = setup_templates(),
check_progress = TRUE) {
c(
if (check_progress) {
tarchetypes::tar_map(
data.frame(project_id = as.character(unique(contents$project_id))),
targets::tar_target_raw(
"progress_hash",
bquote(
fetch_iquizoo(
.(read_file(templates[["progress_hash"]])),
params = list(project_id)
)
),
packages = "tarflow.iquizoo",
cue = targets::tar_cue("always")
)
tar_prep_hash <- function(contents, templates = setup_templates()) {
check_templates(templates)
lapply(
as.character(unique(contents$project_id)),
\(project_id) {
targets::tar_target_raw(
paste0("progress_hash_", project_id),
bquote(
fetch_iquizoo(
.(read_file(templates[["progress_hash"]])),
params = list(.(project_id))
)
),
packages = "tarflow.iquizoo",
cue = targets::tar_cue("always")
)
},
targets::tar_target_raw(
"users",
bquote(
fetch_iquizoo(
.(read_file(templates[["users"]])),
params = list(.(unique(contents$project_id)))
) |>
unique()
),
packages = "tarflow.iquizoo"
)
}
)
}

#' Generate a set of targets for fetching user information
#'
#' The user information is used to identify the users involved in the project.
#'
#' @param contents The contents structure used as the configuration of data
#' fetching.
#' @param templates The SQL template files used to fetch data. See
#' [setup_templates()] for details.
#' @return A list of target objects.
#' @export
tar_fetch_users <- function(contents, templates = setup_templates()) {
check_templates(templates)
targets::tar_target_raw(
"users",
bquote(
fetch_iquizoo(
.(read_file(templates[["users"]])),
params = list(.(unique(contents$project_id)))
) |>
unique()
),
packages = "tarflow.iquizoo"
)
}

Expand All @@ -163,21 +169,22 @@ tar_prep_proj <- function(contents,
#'
#' @param contents The contents structure used as the configuration of data
#' fetching.
#' @param what What to fetch.
#' @param templates The SQL template files used to fetch data. See
#' [setup_templates()] for details.
#' @param what What to fetch.
#' @param check_progress Whether to check the progress hash. If set as `TRUE`,
#' Before fetching the data, the progress hash objects named as
#' `progress_hash_{project_id}` will be depended on, which are typically
#' generated by [tar_prep_proj()]. If the projects are finalized, set this
#' generated by [tar_prep_hash()]. If the projects are finalized, set this
#' argument as `FALSE`.
#' @return A list of target objects.
#' @export
tar_fetch_data <- function(contents,
templates = setup_templates(),
what = c("raw_data", "scores"),
templates = setup_templates(),
check_progress = TRUE) {
what <- match.arg(what)
check_templates(templates)
by(
contents,
contents$game_id,
Expand Down
3 changes: 2 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ reference:
contents:
- use_targets_pipeline
- tar_prep_iquizoo
- tar_prep_proj
- tar_fetch_users
- tar_fetch_data
- tar_prep_raw
- tar_prep_hash
- title: "Low-level Database operations"
desc: Functions to help you interact with database.
contents:
Expand Down
8 changes: 4 additions & 4 deletions man/tar_fetch_data.Rd

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

21 changes: 21 additions & 0 deletions man/tar_fetch_users.Rd

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

25 changes: 25 additions & 0 deletions man/tar_prep_hash.Rd

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

29 changes: 0 additions & 29 deletions man/tar_prep_proj.Rd

This file was deleted.

4 changes: 2 additions & 2 deletions tests/testthat/test-targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ test_that("Workflow works", {
~organization_name, ~project_name,
"北京师范大学", "4.19-4.20夜晚睡眠test"
)
tar_prep_iquizoo(params, combine = "scores")
tarflow.iquizoo::tar_prep_iquizoo(params, combine = "scores")
})
targets::tar_make(reporter = "silent", callr_function = NULL)
expect_silent(targets::tar_make(reporter = "silent"))
expect_snapshot_value(targets::tar_objects(), style = "json2")
expect_snapshot_value(targets::tar_read(users), style = "json2")
expect_snapshot_value(targets::tar_read(scores), style = "json2")
Expand Down

0 comments on commit 255fc81

Please sign in to comment.