-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_targets.R
157 lines (137 loc) · 5.56 KB
/
_targets.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# R notebook source
# -------------------------------------------------------------------------
# Copyright (c) 2022 Craig Robert Shenton. All rights reserved.
# Licensed under the MIT License. See license.txt in the project root for
# license information.
# -------------------------------------------------------------------------
# FILE: _targets.R
# DESCRIPTION: Define target functions
# CONTRIBUTORS: Craig R. Shenton
# CONTACT: [email protected]
# CREATED: 17 Dec 2022
# VERSION: 0.0.2
# Load libraries and global vars
# -------------------------------------------------------------------------
library(targets)
library(tarchetypes)
library(here)
tar_option_set(packages = c("readr",
"here",
"NHSRdatasets",
"dplyr",
"knitr",
"plotly"))
# Load all functions in the utilities folder
# -------------------------------------------------------------------------
source("pkg/R/source_folder.R")
source_folder("pkg/R", recursive = TRUE)
# Start target list
# -------------------------------------------------------------------------
list(
# Ingest ae_attendance data from {NHSRdatasets} package
# -------------------------------------------------------------------------
nhsr_dataset_name <- "ae_attendances",
targets::tar_target(
name = ae_attendance_raw,
command = get_nhsr_dataset(nhsr_dataset_name)
),
# Sink ae_attendance data to raw
# -------------------------------------------------------------------------
targets::tar_target(
name = ae_attendance_raw_sink,
command = write_to_csv(data = ae_attendance_raw,
"data/raw/NHSRdatasets",
"ae_attendances.csv"),
format = "file"
),
# set the period column to show in Month-Year ("%b-%y") format
# -------------------------------------------------------------------------
targets::tar_target(
name = ae_attendance_format_date,
command = dplyr_format_date(data = ae_attendance_raw, "period", "%b-%y")
),
# Sink ae_attendance data to interim
# -------------------------------------------------------------------------
targets::tar_target(
name = ae_attendance_interim_sink,
command = write_to_csv(data = ae_attendance_format_date,
"data/interim/NHSRdatasets",
"ae_attendances.csv"),
format = "file"
),
# filter on attendances
# -------------------------------------------------------------------------
targets::tar_target(
name = ae_attendance_filtered,
command = dplyr_filter_cols(data = ae_attendance_format_date,
attendances > 30000)
),
# write to markdown table
# -------------------------------------------------------------------------
targets::tar_target(
name = ae_attendance_to_markdown_table,
command = knitr_markdown_table(data = ae_attendance_filtered, 10)
),
# Group data by date (i.e., period)
# -------------------------------------------------------------------------
targets::tar_target(
name = england_performance_raw,
command = add_metric_col_percent(data = ae_attendance_raw,
"period",
"breaches",
"attendances",
"performance")
),
# Sink england_performance data to interim
# -------------------------------------------------------------------------
targets::tar_target(
name = england_performance_interim_sink,
command = write_to_csv(data = england_performance_raw,
"data/interim/NHSRdatasets",
"england_performance.csv"),
format = "file"
),
# make ploty line chart
# -------------------------------------------------------------------------
targets::tar_target(
name = england_performance_linechart,
command = plotly_line_chart(data = england_performance_raw,
"period",
"performance",
"NHS England A&E 4 Hour Performance",
"Month of attendance",
"% of attendances that met the 4 hour standard")
),
# set the [period] column to show in Month-Year ("%b-%y") format
# -------------------------------------------------------------------------
targets::tar_target(
name = england_performance_format_date,
command = dplyr_format_date(data = england_performance_raw, "period", "%b-%y")
),
# set the [attendances, breaches] column to 1000s (comma) format
# -------------------------------------------------------------------------
targets::tar_target(
name = england_performance_format_comma,
command = dplyr_format_num(data = england_performance_format_date,
c("attendances", "breaches"), "comma", digits = 1)
),
# set the [performance] column to % (percent) format
# -------------------------------------------------------------------------
targets::tar_target(
name = england_performance_format_percent,
command = dplyr_format_num(data = england_performance_format_comma,
c("performance"), "percent", digits = 1)
),
# write to markdown table
# -------------------------------------------------------------------------
targets::tar_target(
name = england_performance_to_markdown_table,
command = knitr_markdown_table(data = england_performance_format_percent, 10)
),
# Render Quarto template
# -------------------------------------------------------------------------
tarchetypes::tar_quarto(report, here("templates/ae_attendance_min.qmd"),
execute_params = list(test = "test"))
# End target list
# -------------------------------------------------------------------------
)