Skip to content

Commit dbeade4

Browse files
committed
Fixing some return linters
1 parent 02d0ee7 commit dbeade4

File tree

6 files changed

+119
-62
lines changed

6 files changed

+119
-62
lines changed

app/logic/determine_iterations.R

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@ box::use(
66
)
77

88
#' @export
9-
determine_iterations <- function(transformations, scales,
10-
horizon, inp_amount, lstm, epoch, tests, ...) {
9+
determine_iterations <- function(
10+
transformations,
11+
scales,
12+
horizon,
13+
inp_amount,
14+
lstm,
15+
epoch,
16+
tests,
17+
...
18+
) {
1119
inp_amount <- str_split(inp_amount, ",")[[1]]
1220
lstm <- str_split(lstm, ",")[[1]]
1321
parameters <- c(as.list(environment()), list(...))
14-
max_options <- max(unlist(map(parameters, length))
15-
)
16-
iterations <- parameters |>
22+
max_options <- max(unlist(map(parameters, length)))
23+
# iterations
24+
parameters |>
1725
map(rep, length.out = max_options) |>
1826
as_tibble() |>
1927
expand.grid() |>
2028
distinct()
21-
return(iterations)
2229
}

app/logic/model.R

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
box::use(
22
dplyr[across, bind_cols, contains, lead, left_join, matches, mutate, n],
33
dplyr[pull, rename, select, slice],
4-
keras3[compile, fit, keras_input, keras_model, layer_dense, layer_lstm, layer_reshape],
4+
keras3[
5+
compile,
6+
fit,
7+
keras_input,
8+
keras_model,
9+
layer_dense,
10+
layer_lstm,
11+
layer_reshape
12+
],
513
purrr[map, reduce],
614
stats[na.omit, predict],
715
tibble[as_tibble, tibble],
@@ -12,9 +20,13 @@ box::use(
1220
#' @export
1321
multiple_lead <- function(data, n) {
1422
data |>
15-
mutate(across(matches(names(data)), \(x) lead(x, n = n), .names = "lead_{col}_n{n}")) |>
23+
mutate(across(
24+
matches(names(data)),
25+
\(x) lead(x, n = n),
26+
.names = "lead_{col}_n{n}"
27+
)) |>
1628
mutate(index = dplyr::row_number(), .before = value) |>
17-
select(- value)
29+
select(-value)
1830
}
1931

2032
# - Inputs. Gets the data, the amount of historical data to use,
@@ -34,10 +46,10 @@ get_input_vector <- function(data, inp_amount, horizon) {
3446
select(-index) |>
3547
na.omit()
3648
}
37-
inputs <- inputs |>
49+
# inputs
50+
inputs |>
3851
as.matrix() |>
3952
array(c(dim(inputs), 1))
40-
return(inputs)
4153
}
4254

4355
# - Outputs. Gets the data, the amount of historical data to use,
@@ -57,10 +69,10 @@ get_output_vector <- function(data, inp_amount, horizon) {
5769
select(-index) |>
5870
na.omit()
5971
}
60-
outputs <- outputs |>
72+
# outputs
73+
outputs |>
6174
as.matrix() |>
6275
array(c(dim(outputs), 1))
63-
return(outputs)
6476
}
6577

6678
# Getting model. Receive the historical data, the temporal horizon and
@@ -85,7 +97,6 @@ get_model <- function(inp_amount, horizon, lstm) {
8597
# - Iterate along the samples to train and obtain predictions.
8698
#' @export
8799
fit_predict <- function(model, input_vec, output_vec, epoch) {
88-
89100
predictions <- rep(NA, ((dim(output_vec)[1] - 1) * dim(output_vec)[2])) |>
90101
matrix(ncol = dim(output_vec)[2]) |>
91102
array(c((dim(output_vec)[1] - 1), dim(output_vec)[2], dim(output_vec)[3]))
@@ -112,15 +123,16 @@ slide_columns <- function(data, column) {
112123
c(
113124
rep(NA, column - 1),
114125
pull(data, column),
115-
rep(NA, dim(data)[2] - column))
126+
rep(NA, dim(data)[2] - column)
127+
)
116128
}
117129

118130
# Extract predictions as a tibble,
119131
# - If temporal horizon is 1 stay as it is
120132
# - Else find mean value of each time point along the predictions
121133
#' @export
122134
twod_predictions <- function(predictions_3d) {
123-
predictions <- predictions_3d[, , 1] |>
135+
predictions <- predictions_3d[,, 1] |>
124136
as_tibble(.name_repair = "unique")
125137

126138
if (dim(predictions)[2] == 1) {
@@ -150,8 +162,8 @@ model_flow <- function(data, modeldata) {
150162
outputs <- get_output_vector(data, inp_amount, horizon)
151163
model <- get_model(inp_amount, horizon, lstm)
152164
predictions <- fit_predict(model, inputs, outputs, epoch)
153-
predictions <- twod_predictions(predictions)
154-
return(predictions)
165+
# predictions
166+
twod_predictions(predictions)
155167
}
156168

157169
# Test the process of creating and obtaining predictions by the number
@@ -177,8 +189,8 @@ test_model_flow <- function(data, modeldata) {
177189
pred_tests[, i] <- model_flow(data, modeldata)
178190
}
179191

180-
pred_tests <- pred_tests |>
192+
# pred_tests
193+
pred_tests |>
181194
nest() |>
182195
rename(tests = data)
183-
return(pred_tests)
184196
}

app/logic/process.R

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
box::use(
2-
dplyr[across, all_of, arrange, bind_cols, bind_rows, distinct, if_else, left_join],
3-
dplyr[mutate, rename, row_number, select, slice, slice_tail, starts_with, summarize_all],
2+
dplyr[
3+
across,
4+
all_of,
5+
arrange,
6+
bind_cols,
7+
bind_rows,
8+
distinct,
9+
if_else,
10+
left_join
11+
],
12+
dplyr[
13+
mutate,
14+
rename,
15+
row_number,
16+
select,
17+
slice,
18+
slice_tail,
19+
starts_with,
20+
summarize_all
21+
],
422
purrr[map],
523
stats[na.omit],
624
stringr[str_detect],
@@ -9,9 +27,9 @@ box::use(
927
)
1028

1129
box::use(
12-
app/logic/model[test_model_flow],
13-
app/logic/scale[get_actual_scales, get_all_scales],
14-
app/logic/transform[get_actual_serie, get_all_transformations],
30+
app / logic / model[test_model_flow],
31+
app / logic / scale[get_actual_scales, get_all_scales],
32+
app / logic / transform[get_actual_serie, get_all_transformations],
1533
)
1634

1735
# Get data to work with by using the specified sequence and forecast
@@ -22,7 +40,6 @@ box::use(
2240
# "value"
2341
#' @export
2442
get_process_data <- function(data, sequence, forecast) {
25-
2643
if (!any(is.null(sequence), sequence == "")) {
2744
new_data <- data[, c(sequence, forecast)]
2845
names(new_data) <- c("sequence", "value")
@@ -33,8 +50,8 @@ get_process_data <- function(data, sequence, forecast) {
3350
names(new_data) <- "value"
3451
}
3552

36-
new_data <- na.omit(new_data)
37-
return(new_data)
53+
# new_data
54+
na.omit(new_data)
3855
}
3956

4057
# Function to returns min and max value of a column if column exist
@@ -73,15 +90,15 @@ get_all_series <- function(data, transformations, scales) {
7390
se_min_max <- extract_mi_ma(data, "second")
7491

7592
data <- get_all_scales(data, scales)
76-
data <- list(
93+
# data
94+
list(
7795
data = data,
7896
first_diff = first_diff,
7997
second_diff = second_diff,
8098
ex_min_max = ex_min_max,
8199
fi_min_max = fi_min_max,
82100
se_min_max = se_min_max
83101
)
84-
return(data)
85102
}
86103

87104
# Obtain all the tests for all the model configuration (modeldata) for
@@ -97,28 +114,30 @@ get_predict <- function(name, data, modeldata) {
97114
predictions <- tibble(tests_results = rep(NA, dim(modeldata)[1]))
98115

99116
for (i in seq_len(dim(modeldata)[1])) {
100-
predictions[i, ] <- test_model_flow(data |> select(all_of(name)), modeldata[i, ])
117+
predictions[i, ] <- test_model_flow(
118+
data |> select(all_of(name)),
119+
modeldata[i, ]
120+
)
101121
}
102122

103123
predictions <- predictions |>
104124
mutate(index = row_number()) |>
105125
select(index, tests_results)
106126
modeldata <- modeldata |>
107127
mutate(index = row_number())
108-
predictions <- left_join(modeldata, predictions, by = "index") |>
128+
# predictions
129+
left_join(modeldata, predictions, by = "index") |>
109130
select(-index) |>
110131
nest() |>
111132
rename(model_data = data) |>
112133
mutate(data = name) |>
113134
select(data, model_data)
114-
return(predictions)
115135
}
116136

117137
# Function to compute squared error
118138
#' @export
119139
squared_error <- function(x, y) {
120-
z <- (y - x)^2
121-
return(z)
140+
(y - x)^2
122141
}
123142

124143
# Function to get RMSE from test results and give proper format for
@@ -153,7 +172,7 @@ get_results <- function(data, original) {
153172

154173
rmse[i, ] <- bind_cols(tests_results, pred_original) |>
155174
mutate(across(starts_with("test_"), \(x) squared_error(x, value))) |>
156-
select(- value) |>
175+
select(-value) |>
157176
summarize_all(mean) |>
158177
mutate(across(starts_with("test_"), \(x) sqrt(x))) |>
159178
rowMeans()
@@ -179,16 +198,19 @@ get_results <- function(data, original) {
179198
data[i, "tests_results"] <- tests_results
180199
}
181200

182-
data <- bind_cols(data, rmse) |>
201+
# data
202+
bind_cols(data, rmse) |>
183203
mutate(
184204
transformations = str_detect(data, "value") |>
185-
if_else("Original", if_else(str_detect(data, "first"), "First", "Second")),
205+
if_else(
206+
"Original",
207+
if_else(str_detect(data, "first"), "First", "Second")
208+
),
186209
scales = str_detect(data, "z_o") |>
187210
if_else("0 to 1", if_else(str_detect(data, "m_p"), "-1 to 1", "Exact")),
188211
.after = data
189212
) |>
190213
select(-c(data, tests))
191-
return(data)
192214
}
193215

194216
# Execute the process using the previously defined functions. Receive as
@@ -224,8 +246,13 @@ process <- function(data, sequence, forecast, iterations) {
224246
map(\(x) get_predict(name = x, new_data, iterations)) |>
225247
bind_rows()
226248

227-
predictions <- get_actual_scales(predictions, ex_min_max, fi_min_max, se_min_max)
249+
predictions <- get_actual_scales(
250+
predictions,
251+
ex_min_max,
252+
fi_min_max,
253+
se_min_max
254+
)
228255
predictions <- get_actual_serie(predictions, data, first_diff, second_diff)
229-
predictions <- get_results(predictions, data)
230-
return(predictions)
256+
# predictions
257+
get_results(predictions, data)
231258
}

app/logic/scale.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ box::use(
99
# Get scaled series
1010
#' @export
1111
zero_to_one <- function(x) {
12-
y <- rescale(x = x, to = c(0, 1), from = c(min(x), max(x)))
13-
return(y)
12+
rescale(x = x, to = c(0, 1), from = c(min(x), max(x)))
1413
}
1514

1615
#' @export
1716
minus_to_plus <- function(x) {
18-
y <- rescale(x = x, to = c(-1, 1), from = c(min(x), max(x)))
19-
return(y)
17+
rescale(x = x, to = c(-1, 1), from = c(min(x), max(x)))
2018
}
2119

2220
# Apply specified scales to data.
@@ -44,9 +42,9 @@ get_all_scales <- function(data, scales) {
4442
}
4543
scaled <- list(exact, zero_one, minus_plus)
4644
no_null <- !unlist(map(scaled, is.null))
47-
scaled <- scaled[no_null] |>
45+
# scaled
46+
scaled[no_null] |>
4847
bind_cols()
49-
return(scaled)
5048
}
5149

5250
# Rescale tests results to original scale. Receive as input the output
@@ -72,7 +70,8 @@ rescale_back <- function(data, serie, ex_min_max, fi_min_max, se_min_max) {
7270
t_min_max <- se_min_max
7371
}
7472

75-
data <- data |>
73+
# data
74+
data |>
7675
filter(data == serie) |>
7776
unnest(model_data) |>
7877
unnest(tests_results) |>
@@ -84,7 +83,6 @@ rescale_back <- function(data, serie, ex_min_max, fi_min_max, se_min_max) {
8483
) |>
8584
nest(tests_results = starts_with("test_")) |>
8685
nest(model_data = c(horizon, inp_amount, lstm, epoch, tests, tests_results))
87-
return(data)
8886
}
8987

9088
# Execute the previous define function only on those tests results that need to

app/logic/test_id_datatables.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ box::use(
66
# input the table name that should be pass to the testid
77
#' @export
88
test_id_datatables <- function(name) {
9-
jsfunction <- paste0(
9+
# jsfunction
10+
paste0(
1011
"function(settings, json) {",
1112
# Add 'data-testid' to filters
1213
"$(this.api().table().header()).find('tr:eq(1) td').slice(1).each(function(index) {",
@@ -35,5 +36,4 @@ test_id_datatables <- function(name) {
3536
"}"
3637
) |>
3738
JS()
38-
return(jsfunction)
3939
}

0 commit comments

Comments
 (0)