4848# ' )
4949# ' }
5050table_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 ) {
0 commit comments