Skip to content

Commit 8cf18b1

Browse files
committed
style(param_csv): linted
1 parent 951c313 commit 8cf18b1

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

rmarkdown/parameters_csv.Rmd

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@ We use the parameter function to process values from the CSV, ensuring that all
3030
```{r}
3131
#' Use parameter function to create parameter list using values from dataframe.
3232
#'
33-
#' @param df Dataframe with columns "unit", "parameter", "type", "mean" and
33+
#' @param data Dataframe with columns "unit", "parameter", "type", "mean" and
3434
#' "sd".
3535
#' @param unit Unit name to filter by ("asu" or "rehab").
3636
#' @param parameter Parameter name to filter by ("iat", "los" or "routing").
3737
#' @param param_function Function to run
38-
#'
38+
#'
3939
#' @return Named list of parameters
4040
41-
init_param_class <- function(df, unit, parameter, param_function) {
41+
init_param_class <- function(data, unit, parameter, param_function) {
4242
4343
# Filter data to specified unit and parameter
44-
df_subset <- df[df[["unit"]] == unit & df[["parameter"]] == parameter, ]
45-
44+
df_subset <- data[data[["unit"]] == unit &
45+
data[["parameter"]] == parameter, ]
46+
4647
# Create named list of parameter values.
4748
# If all SD values are missing, use only means and name by 'type'.
4849
# Otherwise, include both mean and sd for each type, with names like
@@ -52,9 +53,9 @@ init_param_class <- function(df, unit, parameter, param_function) {
5253
} else {
5354
param_list <- list()
5455
for (i in seq_len(nrow(df_subset))) {
55-
row <- df_subset[i, ]
56-
param_list[[paste0(row$type, "_mean")]] <- row$mean
57-
param_list[[paste0(row$type, "_sd")]] <- row$sd
56+
df_row <- df_subset[i, ]
57+
param_list[[paste0(df_row$type, "_mean")]] <- df_row$mean
58+
param_list[[paste0(df_row$type, "_sd")]] <- df_row$sd
5859
}
5960
}
6061
@@ -64,55 +65,61 @@ init_param_class <- function(df, unit, parameter, param_function) {
6465
```
6566

6667
```{r}
67-
#' Generate named_list with create_parameters() using values loaded from a CSV file.
68+
#' Generate named_list with create_parameters() using values loaded from a CSV
69+
#' file.
70+
#'
71+
#' @param csv_path Path to csv file containing the parameters. Should have
72+
#' columns "unit", "parameter", "type", "mean" and "sd". Missing values should
73+
#' be marked as "NA".
6874
#'
69-
#' @param csv_path Path to csv file containing the parameters. Should have columns "unit", "parameter", "type", "mean" and "sd". Missing values should be marked as "NA".
70-
#'
7175
#' @return Named list generated by create_parameters()
7276
7377
setup_param_from_csv <- function(csv_path) {
7478
# Load parameter data from CSV, treating "NA" as missing values
75-
df <- read.csv(csv_path, na.strings = "NA")
76-
79+
param_data <- read.csv(csv_path, na.strings = "NA")
80+
7781
# Specify mappings of create_parameters() arguments to their corresponding
7882
# units, parameter types and parameter classes
7983
param_specs <- list(
8084
list(name = "asu_arrivals",
8185
unit = "asu",
8286
parameter = "iat",
83-
param_class = create_asu_arrivals),
87+
param_function = create_asu_arrivals),
8488
list(name = "rehab_arrivals",
8589
unit = "rehab",
8690
parameter = "iat",
87-
param_class = create_rehab_arrivals),
91+
param_function = create_rehab_arrivals),
8892
list(name = "asu_los",
8993
unit = "asu",
9094
parameter = "los",
91-
param_class = create_asu_los),
95+
param_function = create_asu_los),
9296
list(name = "rehab_los",
9397
unit = "rehab",
9498
parameter = "los",
95-
param_class = create_rehab_los),
99+
param_function = create_rehab_los),
96100
list(name = "asu_routing",
97101
unit = "asu",
98102
parameter = "routing",
99-
param_class = create_asu_routing),
103+
param_function = create_asu_routing),
100104
list(name = "rehab_routing",
101105
unit = "rehab",
102106
parameter = "routing",
103-
param_class = create_rehab_routing)
107+
param_function = create_rehab_routing)
104108
)
105109
param_kwargs <- list()
106110
for (spec in param_specs) {
107-
param_kwargs[[spec$name]] <- init_param_class(df, spec$unit, spec$parameter, spec$param_class)
111+
param_kwargs[[spec$name]] <- init_param_class(
112+
data = param_data, unit = spec$unit, parameter = spec$parameter,
113+
param_function = spec$param_function
114+
)
108115
}
109116
110117
do.call(create_parameters, param_kwargs)
111118
}
112119
```
113120

114121
```{r}
115-
setup_param_from_csv("../inputs/parameters.csv")
122+
setup_param_from_csv(file.path("..", "inputs", "parameters.csv"))
116123
```
117124

118125
## Calculate run time

rmarkdown/parameters_csv.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@ performed when the function is called.
3737
``` r
3838
#' Use parameter function to create parameter list using values from dataframe.
3939
#'
40-
#' @param df Dataframe with columns "unit", "parameter", "type", "mean" and
40+
#' @param data Dataframe with columns "unit", "parameter", "type", "mean" and
4141
#' "sd".
4242
#' @param unit Unit name to filter by ("asu" or "rehab").
4343
#' @param parameter Parameter name to filter by ("iat", "los" or "routing").
4444
#' @param param_function Function to run
45-
#'
45+
#'
4646
#' @return Named list of parameters
4747

48-
init_param_class <- function(df, unit, parameter, param_function) {
48+
init_param_class <- function(data, unit, parameter, param_function) {
4949

5050
# Filter data to specified unit and parameter
51-
df_subset <- df[df[["unit"]] == unit & df[["parameter"]] == parameter, ]
52-
51+
df_subset <- data[data[["unit"]] == unit &
52+
data[["parameter"]] == parameter, ]
53+
5354
# Create named list of parameter values.
5455
# If all SD values are missing, use only means and name by 'type'.
5556
# Otherwise, include both mean and sd for each type, with names like
@@ -59,9 +60,9 @@ init_param_class <- function(df, unit, parameter, param_function) {
5960
} else {
6061
param_list <- list()
6162
for (i in seq_len(nrow(df_subset))) {
62-
row <- df_subset[i, ]
63-
param_list[[paste0(row$type, "_mean")]] <- row$mean
64-
param_list[[paste0(row$type, "_sd")]] <- row$sd
63+
df_row <- df_subset[i, ]
64+
param_list[[paste0(df_row$type, "_mean")]] <- df_row$mean
65+
param_list[[paste0(df_row$type, "_sd")]] <- df_row$sd
6566
}
6667
}
6768

@@ -71,55 +72,61 @@ init_param_class <- function(df, unit, parameter, param_function) {
7172
```
7273

7374
``` r
74-
#' Generate named_list with create_parameters() using values loaded from a CSV file.
75+
#' Generate named_list with create_parameters() using values loaded from a CSV
76+
#' file.
77+
#'
78+
#' @param csv_path Path to csv file containing the parameters. Should have
79+
#' columns "unit", "parameter", "type", "mean" and "sd". Missing values should
80+
#' be marked as "NA".
7581
#'
76-
#' @param csv_path Path to csv file containing the parameters. Should have columns "unit", "parameter", "type", "mean" and "sd". Missing values should be marked as "NA".
77-
#'
7882
#' @return Named list generated by create_parameters()
7983

8084
setup_param_from_csv <- function(csv_path) {
8185
# Load parameter data from CSV, treating "NA" as missing values
82-
df <- read.csv(csv_path, na.strings = "NA")
83-
86+
param_data <- read.csv(csv_path, na.strings = "NA")
87+
8488
# Specify mappings of create_parameters() arguments to their corresponding
8589
# units, parameter types and parameter classes
8690
param_specs <- list(
8791
list(name = "asu_arrivals",
8892
unit = "asu",
8993
parameter = "iat",
90-
param_class = create_asu_arrivals),
94+
param_function = create_asu_arrivals),
9195
list(name = "rehab_arrivals",
9296
unit = "rehab",
9397
parameter = "iat",
94-
param_class = create_rehab_arrivals),
98+
param_function = create_rehab_arrivals),
9599
list(name = "asu_los",
96100
unit = "asu",
97101
parameter = "los",
98-
param_class = create_asu_los),
102+
param_function = create_asu_los),
99103
list(name = "rehab_los",
100104
unit = "rehab",
101105
parameter = "los",
102-
param_class = create_rehab_los),
106+
param_function = create_rehab_los),
103107
list(name = "asu_routing",
104108
unit = "asu",
105109
parameter = "routing",
106-
param_class = create_asu_routing),
110+
param_function = create_asu_routing),
107111
list(name = "rehab_routing",
108112
unit = "rehab",
109113
parameter = "routing",
110-
param_class = create_rehab_routing)
114+
param_function = create_rehab_routing)
111115
)
112116
param_kwargs <- list()
113117
for (spec in param_specs) {
114-
param_kwargs[[spec$name]] <- init_param_class(df, spec$unit, spec$parameter, spec$param_class)
118+
param_kwargs[[spec$name]] <- init_param_class(
119+
data = param_data, unit = spec$unit, parameter = spec$parameter,
120+
param_function = spec$param_function
121+
)
115122
}
116123

117124
do.call(create_parameters, param_kwargs)
118125
}
119126
```
120127

121128
``` r
122-
setup_param_from_csv("../inputs/parameters.csv")
129+
setup_param_from_csv(file.path("..", "inputs", "parameters.csv"))
123130
```
124131

125132
## $asu_arrivals

0 commit comments

Comments
 (0)