diff --git a/r_app/Rplots.pdf b/r_app/Rplots.pdf index 12e588a..ab6eb21 100644 Binary files a/r_app/Rplots.pdf and b/r_app/Rplots.pdf differ diff --git a/r_app/interpolate_growth_model.R b/r_app/interpolate_growth_model.R index 24bfb56..f298a85 100644 --- a/r_app/interpolate_growth_model.R +++ b/r_app/interpolate_growth_model.R @@ -17,11 +17,15 @@ if (!is.character(project_dir)) { source("parameters_project.R") source("ci_extraction_utils.R") - -pivot_stats2 <- readRDS(here(cumulative_CI_vals_dir,"combined_CI_data.rds")) %>% - ungroup() %>% - group_by(field, sub_field) %>% - summarise(across(everything(), ~ first(na.omit(.))), .groups = "drop") +# Check if the file exists +file_path <- here(cumulative_CI_vals_dir, "combined_CI_data.rds") +pivot_stats2 <- data.frame() +if (file.exists(file_path)) { + pivot_stats2 <- readRDS(file_path) %>% + ungroup() %>% + group_by(field, sub_field) %>% + summarise(across(everything(), ~ first(na.omit(.))), .groups = "drop") +} #%>% drop_na(pivot_quadrant) @@ -61,7 +65,6 @@ extract_CI_data <- function(field_names, harvesting_data, field_CI_data, season) ApproxFun <- approxfun(x = filtered_field_CI_data$Date, y = filtered_field_CI_data$value) Dates <- seq.Date(ymd(min(filtered_field_CI_data$Date)), ymd(max(filtered_field_CI_data$Date)), by = 1) LinearFit <- ApproxFun(Dates) - # Combine interpolated data with the original CI data CI <- data.frame(Date = Dates, FitData = LinearFit) %>% left_join(., filtered_field_CI_data, by = "Date") %>% @@ -71,7 +74,7 @@ extract_CI_data <- function(field_names, harvesting_data, field_CI_data, season) if (nrow(CI) == 0) { return(data.frame()) } - + # Add additional columns if data exists CI <- CI %>% mutate(DOY = seq(1, n(), 1), @@ -93,23 +96,24 @@ CI_all <- map_df(years, function(yr) { filter(year == yr) %>% filter(!is.na(season_start)) %>% pull(sub_field) - + # Filter sub_fields to only include those with value data in pivot_stats_long valid_sub_fields <- sub_fields %>% keep(~ any(pivot_stats_long$sub_field == .x)) - # Extract data for each valid field map(valid_sub_fields, ~ extract_CI_data(.x, harvesting_data = harvesting_data, field_CI_data = pivot_stats_long, season = yr)) %>% list_rbind() }) -CI_all <- CI_all %>% group_by(model) %>% mutate(CI_per_day = FitData - lag(FitData), - cumulative_CI = cumsum(FitData)) - +# it will crash here if CI_all is empty and will not overwrite the rds rendering growth_model.R useless +# if(nrow(CI_all) > 0){ + CI_all <- CI_all %>% + group_by(model) %>% + mutate(CI_per_day = FitData - lag(FitData), cumulative_CI = cumsum(FitData)) +# } message('CI_all cumulative') -head(CI_all) message('show head') saveRDS(CI_all, here(cumulative_CI_vals_dir,"All_pivots_Cumulative_CI_quadrant_year_v2.rds")) diff --git a/r_app/parameters_project.R b/r_app/parameters_project.R index f690109..f69db3a 100644 --- a/r_app/parameters_project.R +++ b/r_app/parameters_project.R @@ -18,6 +18,7 @@ daily_vrt <- here(data_dir, "vrt") harvest_dir <- here(data_dir, "HarvestData") dir.create(here(laravel_storage_dir), showWarnings = FALSE) +dir.create(here(reports_dir), showWarnings = FALSE) dir.create(here(data_dir), showWarnings = FALSE) dir.create(here(log_dir), showWarnings = FALSE) dir.create(here(extracted_CI_dir), showWarnings = FALSE) @@ -67,4 +68,8 @@ log_file <- here(log_dir, paste0(format(Sys.Date(), "%Y%m%d"), ".log")) # Create a logging function log_message <- function(message) { cat(message, "\n", file = log_file, append = TRUE) +} + +log_head <- function(list) { + log_message(paste(capture.output(str(head(AllPivots0))), collapse = "\n")) } \ No newline at end of file