From d37d406b3162fb4b43747d23a2762e492aab6f93 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 4 Apr 2024 16:59:11 +0800 Subject: [PATCH] Ensure templates are created by `setup_templates()` Signed-off-by: Liang Zhang --- R/setup.R | 9 +++++++++ R/targets.R | 14 ++++++-------- man/tar_fetch_data.Rd | 6 +++--- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/R/setup.R b/R/setup.R index 0c184a6..fd52d05 100644 --- a/R/setup.R +++ b/R/setup.R @@ -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" + ) + } +} diff --git a/R/targets.R b/R/targets.R index c666174..bd68c47 100644 --- a/R/targets.R +++ b/R/targets.R @@ -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())) { @@ -123,6 +118,7 @@ tar_prep_iquizoo <- function(params, ..., #' @return A list of target objects. #' @export tar_prep_hash <- function(contents, templates = setup_templates()) { + check_templates(templates) lapply( as.character(unique(contents$project_id)), \(project_id) { @@ -152,6 +148,7 @@ tar_prep_hash <- function(contents, templates = setup_templates()) { #' @return A list of target objects. #' @export tar_fetch_users <- function(contents, templates = setup_templates()) { + check_templates(templates) targets::tar_target_raw( "users", bquote( @@ -172,9 +169,9 @@ tar_fetch_users <- function(contents, templates = setup_templates()) { #' #' @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 @@ -183,10 +180,11 @@ tar_fetch_users <- function(contents, templates = setup_templates()) { #' @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, diff --git a/man/tar_fetch_data.Rd b/man/tar_fetch_data.Rd index 8a52434..aedc4af 100644 --- a/man/tar_fetch_data.Rd +++ b/man/tar_fetch_data.Rd @@ -6,8 +6,8 @@ \usage{ tar_fetch_data( contents, - templates = setup_templates(), what = c("raw_data", "scores"), + templates = setup_templates(), check_progress = TRUE ) } @@ -15,11 +15,11 @@ tar_fetch_data( \item{contents}{The contents structure used as the configuration of data fetching.} +\item{what}{What to fetch.} + \item{templates}{The SQL template files used to fetch data. See \code{\link[=setup_templates]{setup_templates()}} for details.} -\item{what}{What to fetch.} - \item{check_progress}{Whether to check the progress hash. If set as \code{TRUE}, Before fetching the data, the progress hash objects named as \verb{progress_hash_\{project_id\}} will be depended on, which are typically