Skip to content

Commit 4ca07e1

Browse files
committed
fixing outputted column order
1 parent 4f2e803 commit 4ca07e1

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

instat/static/InstatObject/R/instat_object_R6.R

+15-19
Original file line numberDiff line numberDiff line change
@@ -2271,9 +2271,7 @@ DataBook$set("public", "crops_definitions", function(data_name, year, station, r
22712271
i <- 1
22722272

22732273
calculate_rain_condition <- function(data){
2274-
data <- data %>%
2275-
dplyr::select(-rain_total_name) %>%
2276-
unique()
2274+
data <- data %>% dplyr::select(-rain_total_name) %>% unique()
22772275

22782276
for (i in 1:nrow(data)) {
22792277
# Create a condition to filter the daily data based on the year, day, and plant day/length
@@ -2319,28 +2317,24 @@ DataBook$set("public", "crops_definitions", function(data_name, year, station, r
23192317
# we now split by our different rain_total_actual conditions.
23202318
# we do this here to avoid calculating it multiple times.
23212319
for (rain_i in rain_totals){
2322-
filtered_data <- filtered_data_1 %>% dplyr::mutate(rain_total = rain_i) %>%
2323-
dplyr::select(c(rain_total_name, plant_length_name, plant_day_name, dplyr::everything()))
2320+
filtered_data <- filtered_data_1 %>% dplyr::mutate(rain_total = rain_i)
23242321

23252322
# take the rows < 0 and run a check. We want to check
23262323
# if (anyNA(rain_values) && sum_rain < data[[rain_total_name]][i]) { sum_rain <- NA
23272324
# we do this here because we want to avoid running rain_total in calculate_rain_condition for efficiency purposes.
23282325
filtered_data <- filtered_data %>%
23292326
dplyr::mutate(rain_total_actual = ifelse(rain_total_actual < 0, ifelse(-1*rain_total_actual < rain_total, NA, -1*rain_total_actual), rain_total_actual))
23302327

2331-
if (!missing(station)){
2332-
filtered_data <- filtered_data %>%
2333-
dplyr::group_by(.data[[station]], .data[[year]])
2334-
} else {
2335-
filtered_data <- filtered_data %>%
2336-
dplyr::group_by(.data[[year]])
2337-
}
2328+
if (!missing(station)) filtered_data <- filtered_data %>% dplyr::group_by(.data[[station]], .data[[year]])
2329+
else filtered_data <- filtered_data %>% dplyr::group_by(.data[[year]])
23382330

2331+
#return(filtered_data)
2332+
23392333
filtered_data <- filtered_data %>%
23402334
# first add a column (T/F) that states that it is in the rainfall period or not.
2341-
dplyr::mutate(plant_day_cond = start_day <= plant_day,
2342-
length_cond = plant_day + plant_length <= end_day,
2343-
rain_cond = rain_i <= rain_total_actual) %>%
2335+
dplyr::mutate(plant_day_cond = .data[[start_day]] <= plant_day,
2336+
length_cond = plant_day + plant_length <= .data[[end_day]],
2337+
rain_cond = rain_i <= rain_total_actual) %>%
23442338
dplyr::ungroup()
23452339

23462340
if (start_check == "both"){
@@ -2380,9 +2374,12 @@ DataBook$set("public", "crops_definitions", function(data_name, year, station, r
23802374
}
23812375
}
23822376

2377+
if (!missing(station)) column_order <- c(station, plant_day_name, plant_length_name, rain_total_name)
2378+
else column_order <- c(plant_day_name, plant_length_name, rain_total_name)
2379+
23832380
if (return_crops_table){
23842381
# here we get crop_def and import it as a new DF
2385-
crops_def_table <- dplyr::bind_rows(crops_def_table)
2382+
crops_def_table <- dplyr::bind_rows(crops_def_table) %>% dplyr::select(c(all_of(column_order), everything())) %>% dplyr::arrange(dplyr::across(dplyr::all_of(column_order)))
23862383
crops_name <- "crop_def"
23872384
crops_name <- next_default_item(prefix = crops_name, existing_names = self$get_data_names(), include_index = FALSE)
23882385
data_tables <- list(crops_def_table)
@@ -2395,9 +2392,8 @@ DataBook$set("public", "crops_definitions", function(data_name, year, station, r
23952392
self$import_data(data_tables = data_tables)
23962393
}
23972394
if (definition_props){
2398-
if (!missing(station)) prop_data_frame <- dplyr::bind_rows(proportion_df) %>% dplyr::select(c(station, rain_total_name, plant_length_name, plant_day_name, everything()))
2399-
else prop_data_frame <- dplyr::bind_rows(proportion_df) %>% dplyr::select(c(rain_total_name, plant_length_name, plant_day_name, everything()))
2400-
2395+
prop_data_frame <- dplyr::bind_rows(proportion_df) %>% dplyr::select(c(all_of(column_order), everything())) %>% dplyr::arrange(dplyr::across(dplyr::all_of(column_order)))
2396+
24012397
prop_name <- "crop_prop"
24022398
prop_name <- next_default_item(prefix = prop_name, existing_names = self$get_data_names(), include_index = FALSE)
24032399
data_tables <- list(prop_data_frame)

0 commit comments

Comments
 (0)