Skip to content

Commit

Permalink
Merge branch 'release/3.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
psychelzh committed Oct 22, 2023
2 parents 1b028c3 + 668e138 commit 08c0cd8
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 64 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.6.2
Version: 3.7.0
Authors@R: c(
person("Liang", "Zhang", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9041-1150")),
Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
export(check_source)
export(fetch_data)
export(fetch_iquizoo)
export(prepare_fetch_data)
export(preproc_data)
export(setup_option_file)
export(setup_source)
export(setup_templates)
export(use_targets)
export(tar_prep_iquizoo)
export(use_targets_template)
export(wrangle_data)
import(rlang)
import(tidyselect)
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# tarflow.iquizoo 3.7.0

## Breaking Changes

* Renamed `use_targets()` as `use_targets_template()` to avoid name masking with `targets::use_targets()`.
* Renamed `prepare_fetch_data()` as `tar_prep_iquizoo()` to obey the name convention of targets factory.

# tarflow.iquizoo 3.6.2

* Enhance the organization of pkgdown reference.
Expand Down
65 changes: 34 additions & 31 deletions R/targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#'
#' @return NULL (invisible). This function is called for its side effects.
#' @export
use_targets <- function() {
use_targets_template <- function() {
script <- "_targets.R"
if (file.exists(script)) {
cli::cli_alert_info(
Expand All @@ -34,11 +34,12 @@ use_targets <- function() {

# nocov end

#' Prepare targets based on parameters
#' Generate a set of targets for pre-processing of iQuizoo data
#'
#' This target factory prepares a set of target objects used to fetch data from
#' iQuizoo database, separated into static branches so that each is for a
#' specific project and task/game combination.
#' specific project and task/game combination. Further pre-processing on the
#' fetched data can also be added if requested.
#'
#' @param params A [data.frame] or [list] contains the parameters to be bound to
#' the query. Default templates require specifying `organization_name` and
Expand Down Expand Up @@ -69,12 +70,12 @@ use_targets <- function() {
#' if the project is finalized.
#' @return A list of target objects.
#' @export
prepare_fetch_data <- function(params, ...,
contents = NULL,
what = c("all", "raw_data", "scores"),
action_raw_data = c("all", "parse", "none"),
templates = setup_templates(),
check_progress = TRUE) {
tar_prep_iquizoo <- function(params, ...,
contents = NULL,
what = c("all", "raw_data", "scores"),
action_raw_data = c("all", "parse", "none"),
templates = setup_templates(),
check_progress = TRUE) {
check_dots_empty()
if (!inherits(templates, "tarflow.template")) {
cli::cli_abort(
Expand All @@ -99,24 +100,22 @@ prepare_fetch_data <- function(params, ...,
class = "tarflow_bad_contents"
)
}
projects_info <- prepare_pipeline_info(
contents,
templates,
check_progress
)
projects_data <- prepare_pipeline_data(
contents,
templates,
what,
action_raw_data
)
list(
targets::tar_target_raw(
"contents_origin",
expr(unserialize(!!serialize(contents, NULL)))
),
projects_info,
projects_data
tar_projects_info(
contents,
templates,
check_progress
),
tar_projects_data(
contents,
templates,
what,
action_raw_data
)
)
}

Expand Down Expand Up @@ -160,7 +159,7 @@ setup_templates <- function(contents = NULL,
}

# helper functions
prepare_pipeline_info <- function(contents, templates, check_progress) {
tar_projects_info <- function(contents, templates, check_progress) {
targets <- tarchetypes::tar_map(
contents |>
dplyr::distinct(.data$project_id) |>
Expand All @@ -174,6 +173,7 @@ prepare_pipeline_info <- function(contents, templates, check_progress) {
params = list(project_id)
)
),
packages = "tarflow.iquizoo",
cue = targets::tar_cue(if (check_progress) "always")
),
targets::tar_target_raw(
Expand All @@ -183,7 +183,8 @@ prepare_pipeline_info <- function(contents, templates, check_progress) {
!!read_file(templates[["users"]]),
params = list(project_id)
)
)
),
packages = "tarflow.iquizoo"
)
)
)
Expand All @@ -197,13 +198,12 @@ prepare_pipeline_info <- function(contents, templates, check_progress) {
)
}

prepare_pipeline_data <- function(contents, templates,
what, action_raw_data) {
tar_projects_data <- function(contents, templates, what, action_raw_data) {
contents <- contents |>
dplyr::distinct(.data$project_id, .data$game_id)
targets_fetch <- lapply(
what,
set_pipeline_fetch,
tar_fetch_data,
contents = contents,
templates = templates
)
Expand Down Expand Up @@ -238,7 +238,8 @@ prepare_pipeline_data <- function(contents, templates,
if (action_raw_data %in% c("all", "parse")) {
targets::tar_target(
raw_data_parsed,
wrangle_data(raw_data)
wrangle_data(raw_data),
packages = "tarflow.iquizoo"
)
},
if (action_raw_data %in% "all") {
Expand All @@ -249,7 +250,8 @@ prepare_pipeline_data <- function(contents, templates,
raw_data_parsed, prep_fun,
.input = input, .extra = extra
)
}
},
packages = c("tarflow.iquizoo", "preproc.iquizoo")
)
}
)
Expand All @@ -276,7 +278,7 @@ prepare_pipeline_data <- function(contents, templates,
)
}

set_pipeline_fetch <- function(contents, templates, what) {
tar_fetch_data <- function(contents, templates, what) {
key_ids <- c("project_id", "game_id")
tarchetypes::tar_map(
contents |>
Expand All @@ -301,7 +303,8 @@ set_pipeline_fetch <- function(contents, templates, what) {
game_id,
what = !!what
)
})
}),
packages = "tarflow.iquizoo"
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ install.packages("tarflow.iquizoo", repos = c(options("repos"), "https://psychel

## Usage

This simplest way to use this package is to call `tarflow.iquizoo::use_targets()` to generate a {[targets](https://docs.ropensci.org/targets/)} pipeline. After some basic parameters edit (follow the generated script), then you can run the pipeline by executing `targets::tar_make()` in R console. The pipeline will be executed in parallel by default.
This simplest way to use this package is to call `tarflow.iquizoo::use_targets_template()` to generate a {[targets](https://docs.ropensci.org/targets/)} pipeline. After some basic parameters edit (follow the generated script), then you can run the pipeline by executing `targets::tar_make()` in R console. The pipeline will be executed in parallel by default.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ install.packages("tarflow.iquizoo", repos = c(options("repos"), "https://psychel
## Usage

This simplest way to use this package is to call
`tarflow.iquizoo::use_targets()` to generate a
`tarflow.iquizoo::use_targets_template()` to generate a
{[targets](https://docs.ropensci.org/targets/)} pipeline. After some
basic parameters edit (follow the generated script), then you can run
the pipeline by executing `targets::tar_make()` in R console. The
Expand Down
4 changes: 2 additions & 2 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ reference:
- title: "Targets Factory Related Functions"
desc: "A collection of functions to help you build targets."
contents:
- use_targets
- prepare_fetch_data
- use_targets_template
- tar_prep_iquizoo
- title: "Functions Used for Actions on Raw-data"
desc: >
Data parsing and pre-processing. Typically used in combination with
Expand Down
7 changes: 4 additions & 3 deletions inst/pipelines/use_targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ library(targets)

# Set target options:
tar_option_set(
packages = c("tarflow.iquizoo", "preproc.iquizoo"), # packages that your targets need to run
imports = "preproc.iquizoo", # comment out this if only "scores" are required
# packages = "base", # uncomment this if you want to use more packages
# imports = "preproc.iquizoo", # uncomment this if indices will be calculated
# format = "qs", # Optionally set the default storage format. qs is fast.
#
# For distributed computing in tar_make(), supply a {crew} controller
Expand All @@ -35,8 +35,9 @@ params <- tibble::tribble(

# Replace the target list below with your own:
list(
tarflow.iquizoo::prepare_fetch_data(
tarflow.iquizoo::tar_prep_iquizoo(
params,
# contents = NULL, # uncomment this if you want to use custom contents
what = "all", # change to "scores" or "raw_data" if you want to
action_raw_data = "all", # change to "parse" or "none" if you want to
# For advanced usage, set custom templates by uncommenting next line
Expand Down
11 changes: 6 additions & 5 deletions man/prepare_fetch_data.Rd → man/tar_prep_iquizoo.Rd

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

6 changes: 3 additions & 3 deletions man/use_targets.Rd → man/use_targets_template.Rd

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

23 changes: 8 additions & 15 deletions tests/testthat/test-targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ test_that("Default templates work", {
~organization_name, ~project_name,
"北京师范大学", "认知测评预实验"
)
prepare_fetch_data(params) |>
tar_prep_iquizoo(params) |>
expect_targets_list() |>
expect_silent()
})

test_that("Signal error if templates not created correctly", {
templates <- list(contents = "myfile")
prepare_fetch_data(templates = templates) |>
tar_prep_iquizoo(templates = templates) |>
expect_error(class = "tarflow_bad_templates")
})

test_that("Custom templates work", {
prepare_fetch_data(
tar_prep_iquizoo(
data.frame(),
templates = setup_templates(
contents = "sql/contents.sql"
Expand All @@ -27,7 +27,7 @@ test_that("Custom templates work", {
})

test_that("Support `data.frame` contents", {
prepare_fetch_data(
tar_prep_iquizoo(
contents = data.frame(
project_id = bit64::as.integer64(599627356946501),
game_id = bit64::as.integer64(581943246745925)
Expand All @@ -42,21 +42,20 @@ test_that("Signal error if `contents` contains no data", {
~organization_name, ~project_name,
"Unexisted", "Malvalue"
)
prepare_fetch_data(params_bad) |>
tar_prep_iquizoo(params_bad) |>
expect_error(class = "tarflow_bad_contents")
})

test_that("Workflow works", {
targets::tar_dir({
targets::tar_script({
library(targets)
tar_option_set(packages = "tarflow.iquizoo")
params <- tibble::tribble(
~organization_name, ~project_name,
"北京师范大学测试用账号", "难度测试",
"北京师范大学", "4.19-4.20夜晚睡眠test"
)
prepare_fetch_data(params, action_raw_data = "parse")
tar_prep_iquizoo(params, action_raw_data = "parse")
})
targets::tar_make(reporter = "silent", callr_function = NULL)
expect_snapshot_value(targets::tar_objects(), style = "json2")
Expand All @@ -65,15 +64,12 @@ test_that("Workflow works", {
targets::tar_dir({
targets::tar_script({
library(targets)
tar_option_set(
packages = c("tarflow.iquizoo", "preproc.iquizoo")
)
params <- tibble::tribble(
~organization_name, ~project_name,
"北京师范大学测试用账号", "难度测试",
"北京师范大学", "4.19-4.20夜晚睡眠test"
)
prepare_fetch_data(params)
tar_prep_iquizoo(params)
})
targets::tar_make(reporter = "silent", callr_function = NULL)
expect_snapshot_value(targets::tar_objects(), style = "json2")
Expand All @@ -84,14 +80,11 @@ test_that("Serialize check (no roundtrip error)", {
targets::tar_dir({
targets::tar_script({
library(targets)
tar_option_set(
packages = c("tarflow.iquizoo", "preproc.iquizoo")
)
params <- tibble::tribble(
~organization_name, ~project_name,
"四川省双流棠湖中学高中部", "棠湖中学英才计划测训体验账号"
)
prepare_fetch_data(params)[1]
tar_prep_iquizoo(params)[1]
})
targets::tar_make(reporter = "silent", callr_function = NULL)
expect_identical(
Expand Down

0 comments on commit 08c0cd8

Please sign in to comment.