renv::activate() renv::restore() #download excel with planting dates library(googledrive) library(here) library(tidyverse) library(lubridate) library(readxl) #create directory storage_dir <- here("../laravel_app/storage/app") data_dir <- here(storage_dir, "Data") harvest_dir <- here(data_dir, "HarvestData") dir.create(file.path(data_dir)) dir.create(file.path(harvest_dir)) week = ymd(Sys.Date()) file_name <- here(harvest_dir, paste("Data", week, ".xlsx")) # yield_file_path <- here("Data", "HarvestData", paste("Current - Pivots planting date and harvesting data.xlsx")) #Check its existence if (file.exists(file_name)) { #Delete file if it exists file.remove(file_name) print("File deleted") } #options(gargle_oauth_email = "henkpoldergraaf@gmail.com") #folder_url <- "https://docs.google.com/spreadsheets/d/1KPOPHvlzTBDvgFCYQetss0yoafeZeoNW/edit?rtpof=true#gid=1683410275" #folder <- drive_get(as_id(folder_url)) #drive_download(folder, file_name, overwrite = T) excel_file_name = here(storage_dir,"harvesting_data", "Current - Pivots planting date and harevsting data.xlsx") # library(googlesheets4) # read_sheet(folder_url) #get dates in same column dates <- read_excel(excel_file_name, skip = 2, col_types = c("text", "numeric", "text", "date", "numeric", "numeric", "numeric", "date", "numeric", "skip", "skip", "numeric", "numeric", "numeric","skip", #2020 harvesting data "date", "numeric", "skip", "skip", "numeric", "numeric", "numeric","skip", #2021 harvesting data "date", "numeric", "skip", "skip", "numeric", "skip", "numeric","numeric", "skip", #2022 harvesting data "date", "numeric", "skip", "skip", "skip", "skip", "skip", "skip", "skip", "skip", "skip", "skip", "skip", "numeric", "numeric","numeric","skip", #2023 harvesting data "skip", "skip", "skip", "skip", "skip")) %>% #empty columns rename(pivot_quadrant = 1, area = 2, variety = 3, planting_date = 4, Age = 5, ratoon = 6, Year_replanted = 7, Harvesting_date_2020 = 8, Harvesting_age_2020 = 9, MT_weight_2020 = 10, Tcha_2020 = 11, Tchm_2020 = 12, Harvesting_date_2021 = 13, Harvesting_age_2021 = 14, MT_weight_2021 = 15, Tcha_2021 = 16, Tchm_2021 = 17, Harvesting_date_2022 = 18, Harvesting_age_2022 = 19, MT_weight_2022 = 20, Tcha_2022 = 21, Tchm_2022 = 22, Harvesting_date_2023 = 23, Harvesting_age_2023 = 24, MT_weight_2023 = 25, Tcha_2023 = 26, Tchm_2023 = 27, ) %>% # slice(-1) %>% #select(-age) %>% # filter(pivot_quadrant != "Total") %>% #drop_na(pivot_quadrant) %>% mutate(planting_date = ymd(planting_date ), Harvesting_date_2020 = ymd(Harvesting_date_2020), Harvesting_date_2021 = ymd(Harvesting_date_2021), Harvesting_date_2022= ymd(Harvesting_date_2022), Age = round(Age,0)) %>% filter(pivot_quadrant != "Total/Average")%>% filter(pivot_quadrant != "Total") #copy each row and add ABCD quadrants <- dates %>% slice(rep(1:n(), each=4)) %>% group_by(pivot_quadrant) %>% mutate(pivot_quadrant = paste0(pivot_quadrant, c("A", "B", "C", "D"))) %>% filter(pivot_quadrant != "P1.8D" & pivot_quadrant != "P1.8 Q.DA"& pivot_quadrant != "P1.8 Q.DB"& pivot_quadrant != "P1.8 Q.DC") %>% mutate(pivot_quadrant = case_when(pivot_quadrant == "P1.3 ABA" ~ "P1.3A", pivot_quadrant == "P1.3 ABB" ~ "1.3B", pivot_quadrant == "P1.3 ABC" ~ "1.3A", pivot_quadrant == "P1.3 ABD" ~ "1.3A", pivot_quadrant == "P1.3 CDA" ~ "1.3C", pivot_quadrant == "P1.3 CDB" ~ "1.3D", pivot_quadrant == "P1.3 CDC" ~ "1.3C", pivot_quadrant == "P1.3 CDD" ~ "1.3C", pivot_quadrant == "P1.8 Q.DD" ~ "1.8D", pivot_quadrant == "P1.9.ABA" ~ "1.9A", pivot_quadrant == "P1.9.ABB" ~ "1.9B", pivot_quadrant == "P1.9.ABC" ~ "1.9A", pivot_quadrant == "P1.9.ABD" ~ "1.9A", pivot_quadrant == "P1.9.CDA" ~ "1.9C", pivot_quadrant == "P1.9.CDB" ~ "1.9D", pivot_quadrant == "P1.9.CDC" ~ "1.9C", pivot_quadrant == "P1.9.CDD" ~ "1.9C", pivot_quadrant == "P2.3AA" ~ "2.3A", pivot_quadrant == "P2.3AB" ~ "2.3A", pivot_quadrant == "P2.3AC" ~ "2.3A", pivot_quadrant == "P2.3AD" ~ "2.3A", pivot_quadrant == "P2.3DA" ~ "2.3D", pivot_quadrant == "P2.3DB" ~ "2.3D", pivot_quadrant == "P2.3DC" ~ "2.3D", pivot_quadrant == "P2.3DD" ~ "2.3D", pivot_quadrant == "P2.3BCA" ~ "2.3B", pivot_quadrant == "P2.3BCB" ~ "2.3B", pivot_quadrant == "P2.3BCC" ~ "2.3C", pivot_quadrant == "P2.3BCD" ~ "2.3C", pivot_quadrant == "P2.5 ABA" ~ "2.5A", pivot_quadrant == "P2.5 ABB" ~ "2.5B", pivot_quadrant == "P2.5 ABC" ~ "2.5A", pivot_quadrant == "P2.5 ABD" ~ "2.5A", pivot_quadrant == "P2.5 CDA" ~ "2.5C", pivot_quadrant == "P2.5 CDB" ~ "2.5D", pivot_quadrant == "P2.5 CDC" ~ "2.5C", pivot_quadrant == "P2.5 CDD" ~ "2.5C", pivot_quadrant == "P3.1ABA" ~ "3.1A", pivot_quadrant == "P3.1ABB" ~ "3.1B", pivot_quadrant == "P3.1ABC" ~ "3.1A", pivot_quadrant == "P3.1ABD" ~ "3.1A", pivot_quadrant == "P3.1CDA" ~ "3.1C", pivot_quadrant == "P3.1CDB" ~ "3.1D", pivot_quadrant == "P3.1CDC" ~ "3.1C", pivot_quadrant == "P3.1CDD" ~ "3.1C", pivot_quadrant == "P3.2 ABA" ~ "3.2A", pivot_quadrant == "P3.2 ABB" ~ "3.2B", pivot_quadrant == "P3.2 ABC" ~ "3.2A", pivot_quadrant == "P3.2 ABD" ~ "3.2A", pivot_quadrant == "P3.2 CDA" ~ "3.2C", pivot_quadrant == "P3.2 CDB" ~ "3.2D", pivot_quadrant == "P3.2 CDC" ~ "3.2C", pivot_quadrant == "P3.2 CDD" ~ "3.2C", pivot_quadrant == "DL 1.3A" ~ "DL1.3", pivot_quadrant == "DL 1.3B" ~ "DL1.3", pivot_quadrant == "DL 1.3C" ~ "DL1.3", pivot_quadrant == "DL 1.3D" ~ "DL1.3", pivot_quadrant == "DL 1.1A" ~ "DL1.1", pivot_quadrant == "DL 1.1B" ~ "DL1.1", pivot_quadrant == "DL 1.1C" ~ "DL1.1", pivot_quadrant == "DL 1.1D" ~ "DL1.1", TRUE ~ pivot_quadrant) ) %>% unique() %>% mutate_at("pivot_quadrant", str_replace, "P", "") %>% mutate(pivot = pivot_quadrant) %>% mutate_at("pivot",str_replace, "[ABCD]", "") %>% mutate(pivot = case_when(pivot == "L1.1"~"DL1.1", pivot == "L1.3" ~"DL1.3", TRUE ~ pivot)) quadrants2 <- quadrants %>% mutate( season_start_2021 = case_when(!is.na(Harvesting_date_2021) ~ Harvesting_date_2021 - (Harvesting_age_2021 * 30)) , season_end_2021 = case_when(!is.na(Harvesting_date_2021) ~ Harvesting_date_2021), season_start_2022 = case_when(is.na(Harvesting_date_2021) & !is.na(Harvesting_date_2022) ~ Harvesting_date_2022 - (Harvesting_age_2022 * 30), !is.na(Harvesting_date_2021) & !is.na(Harvesting_date_2022) ~ Harvesting_date_2021 ), season_end_2022 = case_when(!is.na(Harvesting_date_2022) & !is.na(season_start_2022) ~ Harvesting_date_2022), season_start_2023 = case_when(ratoon == 0 ~ planting_date, TRUE ~ Harvesting_date_2022), season_start_2023 = case_when(is.na(Harvesting_date_2022) ~ Harvesting_date_2021, TRUE ~ season_start_2023), season_end_2023 = case_when(!is.na(Harvesting_date_2023) ~ Harvesting_date_2023, TRUE ~ ymd(Sys.Date())), season_start_2024 = case_when(!is.na(Harvesting_date_2023) ~ Harvesting_date_2023, TRUE ~ NA), season_end_2024 = case_when(!is.na(season_start_2024) ~ ymd(Sys.Date())), ) saveRDS(quadrants2, here(harvest_dir, "harvest_data_new"))