Skip to content

Commit b2cfe47

Browse files
authored
refactor: ♻️ use sas paths as input in targets template (#244)
# Description Closes #241 Needs a thorough review. ## Checklist - [X] Ran `just run-all` (install-deps fails locally again 👎 )
1 parent 2050919 commit b2cfe47

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

inst/template-targets.R

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
#
44
# Setup:
55
#
6-
# 1. Set the `input_dir` and `output_dir` under "Configuration" below.
6+
# 1. Set `sas_paths` and `output_dir` under "Configuration" below.
77
# 2. Run `targets::tar_make()` (in the same directory) to convert
88
# registers to Parquet.
99
#
10-
# Note: this pipeline re-converts all files on every `tar_make()` call by
11-
# deleting files in the output directory before converting. The main benefit of
12-
# targets here is parallel execution across workers.
10+
# Note: on every `tar_make()` call, the output directory is cleared and all
11+
# SAS files are re-converted. The `sas_paths` target only re-runs when the
12+
# list of input files changes. The main benefit of targets here is parallel
13+
# execution across workers.
1314
#
1415
# For more information on targets, see https://books.ropensci.org/targets/
1516

@@ -18,20 +19,13 @@ library(targets)
1819
# Configuration ----------------------------------------------------------------
1920

2021
config <- list(
21-
# Path to locate SAS files in.
22-
input_dir = "/path/to/register/sas/files/directory",
22+
# Paths to SAS files
23+
sas_paths = list_sas_files("/path/to/sas/directory"),
2324
# Path to output Parquet files in. Parquet files will be located in
2425
# subdirectories of this directory.
2526
output_dir = "/path/to/output/directory"
2627
)
2728

28-
# Check input directory.
29-
if (!dir.exists(config$input_dir)) {
30-
cli::cli_abort(
31-
message = "Input directory does not exist: {config$input_dir}"
32-
)
33-
}
34-
3529
# Target options ---------------------------------------------------------------
3630

3731
tar_option_set(
@@ -61,9 +55,7 @@ tar_option_set(
6155
list(
6256
tar_target(
6357
name = sas_paths,
64-
command = list_sas_files(config$input_dir),
65-
deployment = "main",
66-
cue = tar_cue(mode = "always")
58+
command = config$sas_paths
6759
),
6860

6961
# Empty output directory before writing to avoid outdated Parquet files.

tests/testthat/test-use-targets.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test_that("targets pipeline template converts SAS files to Parquet", {
5454

5555
# Read template and replace placeholder paths.
5656
modified_content <- template_content |>
57-
stringr::str_replace("/path/to/register/sas/files/directory", input_dir) |>
57+
stringr::str_replace("/path/to/sas/directory", input_dir) |>
5858
stringr::str_replace("/path/to/output/directory", output_dir)
5959

6060
# Write and run pipeline.

vignettes/fastreg.qmd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ section:
181181

182182
```{r config}
183183
config <- list(
184-
input_dir = fs::path_temp("sas-dir"),
184+
sas_paths = list_sas_files(fs::path_temp("sas-dir")),
185185
output_dir = fs::path(pipeline_dir, "parquet-registers")
186186
)
187187
```
@@ -203,7 +203,10 @@ targets::tar_make()
203203
#| eval: !expr rlang::is_installed("targets")
204204
template_content <- readLines(fs::path(pipeline_dir, "_targets.R"))
205205
modified_content <- template_content |>
206-
stringr::str_replace("/path/to/register/sas/files/directory", config$input_dir) |>
206+
stringr::str_replace(
207+
"/path/to/sas/directory",
208+
fs::path_temp("sas-dir")
209+
) |>
207210
stringr::str_replace("/path/to/output/directory", config$output_dir)
208211
209212
withr::with_dir(pipeline_dir, {

0 commit comments

Comments
 (0)