Skip to content

Commit 536f66b

Browse files
style and docs: run devtools::document() and style with styler or air
1 parent cc75d54 commit 536f66b

5 files changed

Lines changed: 54 additions & 53 deletions

File tree

R/convert_output.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,9 +2190,9 @@ convert_output <- function(
21902190
# temporarily add call to local csv so I can test
21912191
# con_file <- glue::glue("~/GitHub/stockplotr/inst/resources/{model}_var_names.csv")
21922192
var_names_sheet <- utils::read.csv(con_file, na.strings = "")
2193-
2193+
21942194
if (tolower(model) == "bam") {
2195-
var_names_sheet <- var_names_sheet |>
2195+
var_names_sheet <- var_names_sheet |>
21962196
dplyr::mutate(label = tolower(label))
21972197
}
21982198

R/process_data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ process_table <- function(
533533
dplyr::rename(
534534
!!mod_uncert_lab := uncertainty
535535
) |>
536-
# set values to strings to include trailing zeros from rounding and # format large estimate values with commas
536+
# set values to strings to include trailing zeros from rounding and # format large estimate values with commas
537537
dplyr::mutate(estimate = formatC(estimate, format = "f", digits = digits, big.mark = ",")) |>
538538
tidyr::pivot_wider(
539539
id_cols = dplyr::all_of(c(stringr::str_to_title(mod_cols))),

R/table_index.R

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,17 @@
4848
#' )
4949
#' }
5050
table_index <- function(
51-
dat,
52-
era = NULL,
53-
interactive = TRUE,
54-
group = NULL,
55-
method = "sum",
56-
module = NULL,
57-
label = NULL,
58-
digits = 2,
59-
make_rda = FALSE,
60-
tables_dir = getwd()
61-
) {
62-
51+
dat,
52+
era = NULL,
53+
interactive = TRUE,
54+
group = NULL,
55+
method = "sum",
56+
module = NULL,
57+
label = NULL,
58+
digits = 2,
59+
make_rda = FALSE,
60+
tables_dir = getwd()
61+
) {
6362
# TODO: do group and facet need to be uncommented and updated?
6463
# Filter data for landings
6564
prepared_data <- filter_data(
@@ -71,37 +70,39 @@ table_index <- function(
7170
scale_amount = 1,
7271
interactive = interactive
7372
) |>
74-
dplyr::mutate(estimate = round(as.numeric(estimate), digits = digits),
75-
uncertainty = round(as.numeric(uncertainty), digits = digits))
76-
73+
dplyr::mutate(
74+
estimate = round(as.numeric(estimate), digits = digits),
75+
uncertainty = round(as.numeric(uncertainty), digits = digits)
76+
)
77+
7778
# Add check if there is any data
7879
if (nrow(prepared_data) == 0) {
7980
cli::cli_abort("No index data found.")
8081
}
81-
82+
8283
# get uncertainty label by model
8384
uncert_lab <- prepared_data |>
8485
dplyr::filter(!is.na(uncertainty_label)) |>
8586
dplyr::group_by(model) |>
8687
dplyr::reframe(unique_uncert = unique(uncertainty_label)) # changed to reframe -- may cause errors
8788
uncert_lab <- stats::setNames(uncert_lab$unique_uncert, uncert_lab$model)
8889
# if (length(unique(uncert_lab)) == 1) uncert_lab <- unique(uncert_lab) # might need this line
89-
90+
9091
# This needs to be adjusted when comparing different models and diff error
9192
if (length(uncert_lab) > 1 & length(unique(uncert_lab)) == 1 | length(names(uncert_lab)) == 1) { # prepared_data$model
9293
# cli::cli_alert_warning("More than one value for uncertainty exists: {uncert_lab}")
9394
uncert_lab <- uncert_lab[[1]]
9495
# cli::cli_alert_warning("The first value ({uncert_lab}) will be chosen.")
9596
}
96-
97+
9798
if (is.na(uncert_lab)) uncert_lab <- "uncertainty"
98-
99+
99100
# get fleet names
100101
# TODO: change from fleets to id_group AFTER the process data step and adjust throughout the table based on indexing
101102
fleets <- unique(prepared_data$fleet) |>
102103
# sort numerically even if fleets are 100% characters
103104
stringr::str_sort(numeric = TRUE)
104-
105+
105106
# TODO: fix this so that fleet names aren't removed if, e.g., group = "fleet"
106107
table_data_info <- process_table(
107108
dat = prepared_data,
@@ -113,44 +114,43 @@ table_index <- function(
113114
table_data <- table_data_info[[1]]
114115
indexed_vars <- table_data_info[[2]]
115116
id_col_vals <- table_data_info[[3]]
116-
117+
117118
# id_group_vals <- sapply(id_cols, function(x) unique(prepared_data[[x]]), simplify = FALSE)
118119
# TODO: add check if there is a index column for every error column -- if not remove the error (can keep index)
119-
120+
120121
# if (uncert_lab != "") uncert_lab <- glue::glue("({uncert_lab})")
121-
122+
122123
# merge error and index columns and rename
123124
df_list <- merge_error(
124125
table_data,
125126
id_col_vals,
126127
unit_label = "", # should this be CPUE?
127128
uncert_lab
128129
)
129-
130+
130131
# transform dfs into tables
131132
final <- lapply(df_list, function(df) {
132133
df |>
133134
gt::gt() |>
134135
add_theme()
135136
})
136-
137+
137138
# export figure to rda if argument = T
138139
if (make_rda == TRUE) {
139-
140140
# Caption contains no key quantities for index table
141141
# So, export captions/alt text csv if absent
142142
if (!file.exists(fs::path(getwd(), "captions_alt_text.csv"))) {
143143
caps_alttext <- utils::read.csv(
144144
system.file("resources", "captions_alt_text_template.csv", package = "stockplotr")
145145
)
146-
# export df with captions and alt text to csv
147-
utils::write.csv(
148-
x = caps_alttext,
149-
file = fs::path(getwd(), "captions_alt_text.csv"),
150-
row.names = FALSE
151-
)
146+
# export df with captions and alt text to csv
147+
utils::write.csv(
148+
x = caps_alttext,
149+
file = fs::path(getwd(), "captions_alt_text.csv"),
150+
row.names = FALSE
151+
)
152152
}
153-
153+
154154
if (length(df_list) == 1) {
155155
create_rda(
156156
object = final$label,
@@ -168,7 +168,7 @@ table_index <- function(
168168
cli::cli_alert_warning("Multiple tables cannot be exported at this time.")
169169
cli::cli_alert_info("We are currently developing this feature.")
170170
}
171-
171+
172172
# Send table(s) to viewer
173173
if (!is.data.frame(table_data)) {
174174
for (t in final) {

R/utils_table.R

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ check_label_differences <- function(dat, index_variables, id_group = NULL) {
194194
#' to reduce redundancy in the table.
195195
#'
196196
merge_error <- function(
197-
table_data,
198-
id_col_vals,
199-
unit_label,
200-
uncert_lab
201-
) {
197+
table_data,
198+
id_col_vals,
199+
unit_label,
200+
uncert_lab
201+
) {
202202
# TODO: change fleets to grouping when the data is indexed by factors other than fleet
203203
lapply(table_data, function(tab_dat) {
204204
label_cols <- names(tab_dat)[-c(1, grep(glue::glue("^{uncert_lab}"), names(tab_dat)))]
@@ -211,12 +211,13 @@ merge_error <- function(
211211
tolower(l_col),
212212
paste(
213213
stringr::str_escape(unlist(id_col_vals, use.names = FALSE)),
214-
collapse = "|")
214+
collapse = "|"
215215
)
216-
216+
)
217+
217218
# Identify which uncert col aligns with l_col
218219
uncert_col <- uncert_cols[grep(l_col, uncert_cols)]
219-
220+
220221
# adjust tab dat to combine the uncert_col value into the l_col = l_col (uncert_col)
221222
tab_dat <- tab_dat |>
222223
dplyr::mutate(
@@ -226,21 +227,21 @@ merge_error <- function(
226227
# maybe not good practice to insert dash?
227228
# ifelse(
228229
# is.na(.data[[l_col]]),
229-
"-"
230+
"-"
230231
# as.character(.data[[l_col]])
231232
# )
232233
)
233234
) |>
234235
# Remove uncertainty colummn id'd in this step of the loop
235236
dplyr::select(-dplyr::all_of(uncert_col))
236237
} # close loop combining label and uncertainty
237-
238+
238239
# Adjust all header label names now
239240
header_labs <- stringr::str_replace_all(colnames(tab_dat), "_", " ") |>
240241
stringr::str_to_title()
241242
header_labs2 <- glue::glue("{header_labs[-1]}{ifelse(unit_label!='', paste0(' ', unit_label,' '), ' ')}({uncert_lab})")
242243
colnames(tab_dat) <- c(header_labs[1], header_labs2)
243-
244+
244245
return(tab_dat)
245246
}) # close and end lapply
246247
}

tests/testthat/test-table_index.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test_that("table_index generates plots without errors", {
66
interactive = FALSE
77
)
88
)
9-
9+
1010
# expect error-free plot with many arguments
1111
expect_no_error(
1212
table_index(
@@ -15,8 +15,8 @@ test_that("table_index generates plots without errors", {
1515
tables_dir = getwd()
1616
)
1717
)
18-
19-
18+
19+
2020
# expect gt object is returned
2121
# adjust this test to work for multiple output tables
2222
# expect_s3_class(
@@ -40,11 +40,11 @@ test_that("rda file made when indicated", {
4040
make_rda = TRUE,
4141
tables_dir = getwd()
4242
)
43-
43+
4444
# expect that both tables dir and the index_table.rda file exist
4545
expect_true(dir.exists(fs::path(getwd(), "tables")))
4646
expect_true(file.exists(fs::path(getwd(), "tables", "index_table.rda")))
47-
47+
4848
# erase temporary testing files
4949
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
5050
unlink(fs::path(getwd(), "tables"), recursive = T)

0 commit comments

Comments
 (0)