|
| 1 | +library(shiny) |
| 2 | +library(readr) |
| 3 | +library(raster) |
| 4 | +library(DT) |
| 5 | + |
| 6 | +# Define the UI |
| 7 | +ui <- fluidPage( |
| 8 | + titlePanel("Crop and Country Selection"), |
| 9 | + sidebarLayout( |
| 10 | + sidebarPanel( |
| 11 | + selectInput("crop", "Select Crop:", choices = c("WHEA", "RICE", "MAIZ", "BARL", "PMIL", "SMIL")), |
| 12 | + selectInput("country", "Select Country:", choices = c("NGA", "CMR", "TZA", "COD", "AGO", "COG")), |
| 13 | + selectInput("agricultural_output", "Select Output Type:", choices = c("PRODUCTIVITY", "YIELD", "HARV_AREA", "PHYS_AREA", "VAL_PROD")), |
| 14 | + br(), |
| 15 | + downloadButton("download_csv", "Download CSV"), |
| 16 | + actionButton("generate_plot", "Generate Raster Plot"), |
| 17 | + plotOutput("raster_plot") |
| 18 | + ), |
| 19 | + mainPanel( |
| 20 | + textOutput("selected_options"), |
| 21 | + dataTableOutput("data_table") |
| 22 | + ) |
| 23 | + ) |
| 24 | +) |
| 25 | + |
| 26 | +# Define the server logic |
| 27 | +server <- function(input, output) { |
| 28 | + |
| 29 | + # Display selected options |
| 30 | + output$selected_options <- renderText({ |
| 31 | + paste("Selected Crop:", input$crop, "\n", |
| 32 | + "Selected Country:", input$country, "\n", |
| 33 | + "Selected Output Type:", input$agricultural_output) |
| 34 | + }) |
| 35 | + |
| 36 | + # Create a reactive data frame |
| 37 | + data <- reactive({ |
| 38 | + bd <- raster::getData('GADM', country = input$country, level = 1) |
| 39 | + ad <- length(bd) |
| 40 | + |
| 41 | + thetahat <- matrix(NA, ad, 1) |
| 42 | + names <- matrix(NA, ad, 2) |
| 43 | + |
| 44 | + x <- ifelse(input$agricultural_output == "PRODUCTIVITY", |
| 45 | + paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_prod.geotiff/spam2010V2r0_global_P_", input$crop, "_A.tif"), |
| 46 | + ifelse(input$agricultural_output == "YIELD", |
| 47 | + paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_yield.geotiff/spam2010V2r0_global_Y_", input$crop, "_A.tif"), |
| 48 | + ifelse(input$agricultural_output == "HARV_AREA", |
| 49 | + paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_harv_area.geotiff/spam2010V2r0_global_H_", input$crop, "_A.tif"), |
| 50 | + ifelse(input$agricultural_output == "PHYS_AREA", |
| 51 | + paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_phys_area.geotiff/spam2010V2r0_global_A_", input$crop, "_A.tif"), "")))) |
| 52 | + |
| 53 | + raster1 <- raster(here::here(x)) |
| 54 | + bd_raster <- crop(raster1, extent(bd)) |
| 55 | + bd_raster2 <- mask(bd_raster, bd) |
| 56 | + |
| 57 | + d <- extract(x = bd_raster2, y = bd, fun = mean, na.rm = TRUE, sp = TRUE) |
| 58 | + thetahat[, 1] <- d@data[, 11] #was 13 before |
| 59 | + names[, 1] <- d@data[, 2] |
| 60 | + names[, 2] <- d@data[, 4] |
| 61 | + mat <- cbind(names, thetahat) |
| 62 | + |
| 63 | + data <- as.data.frame(mat) |
| 64 | + data |
| 65 | + }) |
| 66 | + |
| 67 | + # Render the data table |
| 68 | + output$data_table <- renderDataTable({ |
| 69 | + data() |
| 70 | + }) |
| 71 | + |
| 72 | + # Render the raster plot |
| 73 | + output$raster_plot <- renderPlot({ |
| 74 | + bd <- raster::getData('GADM', country = input$country, level = 1) |
| 75 | + |
| 76 | + x <- ifelse(input$agricultural_output == "PRODUCTIVITY", |
| 77 | + paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_prod.geotiff/spam2010V2r0_global_P_", input$crop, "_A.tif"), |
| 78 | + ifelse(input$agricultural_output == "YIELD", |
| 79 | + paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_yield.geotiff/spam2010V2r0_global_Y_", input$crop, "_A.tif"), |
| 80 | + ifelse(input$agricultural_output == "HARV_AREA", |
| 81 | + paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_harv_area.geotiff/spam2010V2r0_global_H_", input$crop, "_A.tif"), |
| 82 | + ifelse(input$agricultural_output == "PHYS_AREA", |
| 83 | + paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_phys_area.geotiff/spam2010V2r0_global_A_", input$crop, "_A.tif"), "")))) |
| 84 | + |
| 85 | + raster1 <- raster(here::here(x)) |
| 86 | + bd_raster <- crop(raster1, extent(bd)) |
| 87 | + bd_raster2 <- mask(bd_raster, bd) |
| 88 | + |
| 89 | + |
| 90 | + if (input$generate_plot > 0) { |
| 91 | + plot(bd) |
| 92 | + plot(bd_raster2, add = TRUE) |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + # Download the data as a CSV file |
| 97 | + output$download_csv <- downloadHandler( |
| 98 | + filename = function() { |
| 99 | + paste("crop_country_data", Sys.Date(), ".csv", sep = "") |
| 100 | + }, |
| 101 | + content = function(file) { |
| 102 | + write_csv(data(), file) |
| 103 | + } |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +# Run the application |
| 108 | +shinyApp(ui = ui, server = server) |
0 commit comments