Skip to content

Commit 71dfb7a

Browse files
committed
Use check_exclusive() to enhance API
Signed-off-by: Liang Zhang <[email protected]>
1 parent b483490 commit 71dfb7a

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

R/targets.R

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
#' specific project and task/game combination. Further pre-processing on the
66
#' fetched data can also be added if requested.
77
#'
8-
#' @param params A [data.frame] or [list] contains the parameters to be bound to
9-
#' the query. Default templates require specifying `organization_name` and
10-
#' `project_name`, in that order. If `contents` template is specified without
11-
#' any parameters, set it as empty vector or `NULL`. If `contents` argument is
12-
#' specified, this argument is omitted.
8+
#' @param params,contents Used as the configuration of data fetching. These two
9+
#' arguments are mutually exclusive. If `params` is specified, it will be used
10+
#' as parameters to be bound to the query, see [DBI::dbBind()] for more
11+
#' details. The default template requires specifying `organization_name` and
12+
#' `project_name`, in that order. If `contents` is specified, it should be a
13+
#' [data.frame] and will be used directly as the configuration of data
14+
#' fetching. Note `contents` should at least contain `project_id` and
15+
#' `game_id` names.
1316
#' @param ... For future usage. Should be empty.
14-
#' @param contents The contents structure used as the configuration of data
15-
#' fetching. It is typically automatically fetched from database based on the
16-
#' `contents` template in `templates`. If not `NULL`, it will be used directly
17-
#' and ignore that specified in `templates`. Note `contents` should at least
18-
#' contains `project_id` and `game_id` names.
1917
#' @param what What to fetch. There are basically two types of data, i.e., raw
2018
#' data and scores. The former is the logged raw data for each trial of the
2119
#' tasks/games, while the latter is the scores calculated by iQuizoo server.
@@ -29,23 +27,37 @@
2927
#' done. If set as "none", neither will be done. If `what` is "scores", this
3028
#' argument will be ignored.
3129
#' @param combine Specify which targets to be combined. Note you should only
32-
#' specify names from `c("scores", "raw_data", "raw_data_parsed",
33-
#' "indices")`. If `NULL`, none will be combined.
30+
#' specify names from `c("scores", "raw_data", "raw_data_parsed", "indices")`.
31+
#' If `NULL`, none will be combined.
3432
#' @param templates The SQL template files used to fetch data. See
3533
#' [setup_templates()] for details.
3634
#' @param check_progress Whether to check the progress hash. Set it as `FALSE`
3735
#' if the project is finalized.
3836
#' @return A list of target objects.
3937
#' @export
40-
tar_prep_iquizoo <- function(params, ...,
41-
contents = NULL,
38+
tar_prep_iquizoo <- function(params, contents, ...,
4239
what = c("raw_data", "scores"),
4340
action_raw_data = c("all", "parse", "none"),
4441
combine = NULL,
4542
templates = setup_templates(),
4643
check_progress = TRUE) {
4744
check_dots_empty()
4845
check_templates(templates)
46+
contents <- switch(check_exclusive(params, contents),
47+
params = fetch_iquizoo_mem()(
48+
read_file(templates$contents),
49+
params = unname(
50+
if (!is_empty(params)) as.list(params)
51+
)
52+
),
53+
contents = {
54+
stopifnot(
55+
"`content` must be a `data.frame`." =
56+
is.data.frame(contents)
57+
)
58+
contents
59+
}
60+
)
4961
what <- match.arg(what, several.ok = TRUE)
5062
action_raw_data <- match.arg(action_raw_data)
5163
if (!is.null(combine) && !all(combine %in% objects())) {
@@ -54,14 +66,6 @@ tar_prep_iquizoo <- function(params, ...,
5466
class = "tarflow_bad_combine"
5567
)
5668
}
57-
if (is.null(contents)) {
58-
contents <- fetch_iquizoo_mem()(
59-
read_file(templates$contents),
60-
params = unname(
61-
if (!is_empty(params)) as.list(params)
62-
)
63-
)
64-
}
6569
if (nrow(contents) == 0) {
6670
cli::cli_abort(
6771
"No contents to fetch.",

man/tar_prep_iquizoo.Rd

Lines changed: 11 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/targets.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
{
4343
"type": "character",
4444
"attributes": {},
45-
"value": ["陈佳洋", "茹祎", "于胜蓝", "李荟镕", "张忱涵", "连宗鸿", "徐小琳"]
45+
"value": ["徐小琳", "连宗鸿", "张忱涵", "李荟镕", "于胜蓝", "茹祎", "陈佳洋"]
4646
},
4747
{
4848
"type": "integer",
@@ -58,7 +58,7 @@
5858
"value": ["Date"]
5959
}
6060
},
61-
"value": [-25202, -25201, -25200, -25199, -25198, -25197, -25196]
61+
"value": [-25196, -25197, -25198, -25199, -25200, -25201, -25202]
6262
},
6363
{
6464
"type": "character",

tests/testthat/test-targets.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test_that("Default templates work", {
1010

1111
test_that("Signal error if templates not created correctly", {
1212
templates <- list(contents = "myfile")
13-
tar_prep_iquizoo(templates = templates) |>
13+
tar_prep_iquizoo(NULL, templates = templates) |>
1414
expect_error(class = "tarflow_bad_templates")
1515
})
1616

0 commit comments

Comments
 (0)