|
| 1 | +library(shiny) |
| 2 | +library(dplyr) |
| 3 | +library(raster) |
| 4 | +library(googledrive) |
| 5 | + |
| 6 | +# Define the UI |
| 7 | +ui <- fluidPage( |
| 8 | + titlePanel("Spatial Production Crop, Country, and Output Selection"), |
| 9 | + sidebarLayout( |
| 10 | + sidebarPanel( |
| 11 | + selectizeInput("countries", "Add Countries:", choices = c("NGA", "CMR", "TZA", "COD"), multiple = TRUE), |
| 12 | + conditionalPanel( |
| 13 | + condition = "input.countries.length > 0", |
| 14 | + selectizeInput("crop", "Select Crop:", choices = c("WHEA", "RICE", "MAIZ", "BARL"), multiple = TRUE), |
| 15 | + selectizeInput("output_type", "Select Output Type:", choices = c("PRODUCTIVITY", "YIELD"), multiple = TRUE) |
| 16 | + ), |
| 17 | + br(), |
| 18 | + downloadButton("download_csv", "Download CSV") |
| 19 | + ), |
| 20 | + mainPanel( |
| 21 | + dataTableOutput("result_table") |
| 22 | + ) |
| 23 | + ) |
| 24 | +) |
| 25 | + |
| 26 | +# Define the server logic |
| 27 | +server <- function(input, output) { |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + read_raster_from_drive <- function(folder_url, crop_chosen, output) { |
| 32 | + |
| 33 | + #drive_auth(cache = ".secrets") |
| 34 | + drive_auth(cache = ".secrets", email = TRUE, use_oob = TRUE) |
| 35 | + |
| 36 | + folder <- drive_get(as_id(folder_url)) |
| 37 | + files <- drive_ls(folder$id) |
| 38 | + |
| 39 | + file_name <- ifelse(output == "PRODUCTIVITY", |
| 40 | + paste0("spam2017V2r1_SSA_P_", crop_chosen, "_A.tif"), |
| 41 | + ifelse(output == "YIELD", |
| 42 | + paste0("spam2017V2r1_SSA_Y_", crop_chosen, "_A.tif"), "")) |
| 43 | + |
| 44 | + |
| 45 | + #file_name <- paste0("spam2017V2r1_SSA_P_", crop_chosen, "_A.tif") |
| 46 | + |
| 47 | + file <- files[which(files$name == file_name),] |
| 48 | + |
| 49 | + if (length(file) == 0) { |
| 50 | + print("File not found.") |
| 51 | + } else { |
| 52 | + |
| 53 | + temp_file <- tempfile(fileext = ".tif") |
| 54 | + x <- drive_download(as_id(file$id), path = temp_file) |
| 55 | + |
| 56 | + raster1 <- raster(here::here(x$local_path)) |
| 57 | + |
| 58 | + return(raster1) |
| 59 | + |
| 60 | + file.remove(temp_file) |
| 61 | + |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + # Function to extract data for a single country and crop |
| 69 | + extractData <- function(country, crop, output_type) { |
| 70 | + bd <- raster::getData('GADM', country = country, level = 1) |
| 71 | + ad <- length(bd) |
| 72 | + |
| 73 | + thetahat <- vector("numeric", ad) |
| 74 | + names1 <- vector("character", ad) |
| 75 | + names2 <- vector("character", ad) |
| 76 | + |
| 77 | + folder_url <- ifelse(output_type == "PRODUCTIVITY", "https://drive.google.com/drive/folders/1FqeBF-y4iMMHIxCEybXAlwqPJ7KSJkd5", |
| 78 | + ifelse(output_type == "YIELD", "https://drive.google.com/drive/folders/11imEWGPvGsesshkPuwj3s7_lWHUiPleY", "")) |
| 79 | + |
| 80 | + raster1 <- read_raster_from_drive(folder_url=folder_url, crop_chosen=crop, output=output_type) |
| 81 | + |
| 82 | + #raster1 <- raster(here::here(x)) |
| 83 | + bd_raster <- crop(raster1, extent(bd)) |
| 84 | + bd_raster2 <- mask(bd_raster, bd) |
| 85 | + |
| 86 | + d <- extract(x = bd_raster2, y = bd, fun = mean, na.rm = TRUE, sp = TRUE) |
| 87 | + thetahat <- d@data[, 11] # was 13 before |
| 88 | + names1 <- d@data[, 2] |
| 89 | + names2 <- d@data[, 4] |
| 90 | + |
| 91 | + data <- data.frame(Country = rep(country, ad), |
| 92 | + Crop = rep(crop, ad), |
| 93 | + stringsAsFactors = FALSE) |
| 94 | + |
| 95 | + data$District <- names2 |
| 96 | + data[[output_type]] <- thetahat |
| 97 | + |
| 98 | + data |
| 99 | + } |
| 100 | + |
| 101 | + # Create a reactive data frame |
| 102 | + result <- reactive({ |
| 103 | + countries <- input$countries |
| 104 | + crops <- input$crop |
| 105 | + output_types <- input$output_type |
| 106 | + |
| 107 | + data <- data.frame(Country = character(), |
| 108 | + Crop = character(), |
| 109 | + stringsAsFactors = FALSE) |
| 110 | + |
| 111 | + for (country in countries) { |
| 112 | + for (crop in crops) { |
| 113 | + row <- data.frame(Country = country, |
| 114 | + Crop = crop, |
| 115 | + stringsAsFactors = FALSE) |
| 116 | + |
| 117 | + for (output_type in output_types) { |
| 118 | + extracted_data <- extractData(country, crop, output_type) |
| 119 | + col_name <- output_type |
| 120 | + |
| 121 | + row <- do.call("rbind", replicate(n=nrow(extracted_data), row, simplify = FALSE)) #added this myself |
| 122 | + |
| 123 | + row <- row[1:nrow(extracted_data),] |
| 124 | + |
| 125 | + row$District <- extracted_data$District |
| 126 | + row[[col_name]] <- extracted_data[[col_name]] |
| 127 | + |
| 128 | + row %>% relocate(District, .after=Country) |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + data <- rbind(data, row) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + data |
| 137 | + }) |
| 138 | + |
| 139 | + # Render the result table |
| 140 | + output$result_table <- renderDataTable({ |
| 141 | + result() |
| 142 | + }) |
| 143 | + |
| 144 | + # Download the data as a CSV file |
| 145 | + output$download_csv <- downloadHandler( |
| 146 | + filename = function() { |
| 147 | + paste("crop_country_data", Sys.Date(), ".csv", sep = "") |
| 148 | + }, |
| 149 | + content = function(file) { |
| 150 | + write.csv(result(), file, row.names = FALSE) |
| 151 | + } |
| 152 | + ) |
| 153 | +} |
| 154 | + |
| 155 | +# Run the application |
| 156 | +shinyApp(ui = ui, server = server) |
0 commit comments