Skip to content

Commit 4e9a456

Browse files
authored
Add files via upload
1 parent 6a55aa0 commit 4e9a456

File tree

4 files changed

+466
-0
lines changed

4 files changed

+466
-0
lines changed

Diff for: rshiny.R

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
library(shiny)
2+
library(readr)
3+
library(raster)
4+
5+
# Define the UI
6+
ui <- fluidPage(
7+
titlePanel("Crop and Country Selection"),
8+
sidebarLayout(
9+
sidebarPanel(
10+
selectInput("crop", "Select Crop:", choices = c("WHEA", "RICE")),
11+
selectInput("country", "Select Country:", choices = c("NGA", "CMR")),
12+
selectInput("agricultural_output", "Select Output Type:", choices = c("PRODUCTIVITY", "YIELD")),
13+
br(),
14+
downloadButton("download_csv", "Download CSV")
15+
),
16+
mainPanel(
17+
textOutput("selected_options")
18+
)
19+
)
20+
)
21+
22+
# Define the server logic
23+
server <- function(input, output) {
24+
25+
26+
# Display selected options
27+
output$selected_options <- renderText({
28+
paste("Selected Crop:", input$crop, "\n",
29+
"Selected Country:", input$country, "\n",
30+
"Selected Output Type:", input$agricultural_output)
31+
})
32+
33+
34+
# Create a reactive data frame
35+
data <- reactive({
36+
37+
bd <- raster::getData('GADM', country=input$country, level=1)
38+
ad <- length(bd)
39+
40+
thetahat=matrix(NA,ad,1)
41+
names=matrix(NA,ad,2)
42+
43+
#x <- paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_prod.geotiff/spam2010V2r0_global_P_", input$crop, "_A.tif")
44+
45+
46+
47+
x <- ifelse(input$agricultural_output == "PRODUCTIVITY", paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_prod.geotiff/spam2010V2r0_global_P_", input$crop, "_A.tif"),
48+
ifelse(input$agricultural_output == "YIELD", paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_yield.geotiff/spam2010V2r0_global_Y_", input$crop, "_A.tif"), ""))
49+
50+
51+
52+
raster1 <- raster(here::here(x)) # load file
53+
# apply function
54+
55+
bd_raster <- crop(raster1, extent(bd))
56+
#plot(bd_raster)
57+
bd_raster2 <- mask(bd_raster, bd)
58+
#plot(bd_raster2)
59+
#plot(bd, add=TRUE)
60+
61+
d <- extract(x=bd_raster2, y=bd, fun=mean, na.rm=TRUE, sp=T)
62+
thetahat[,1]=d@data[,11] #was 13 before
63+
names[,1]=d@data[,2]
64+
names[,2]=d@data[,4]
65+
mat <- cbind(names, thetahat)
66+
67+
data <- as.data.frame(mat)
68+
69+
})
70+
71+
72+
73+
# Download the data as a CSV file
74+
output$download_csv <- downloadHandler(
75+
filename = function() {
76+
paste("crop_country_data", Sys.Date(), ".csv", sep = "")
77+
},
78+
content = function(file) {
79+
write_csv(data(), file)
80+
}
81+
)
82+
}
83+
84+
# Run the application
85+
shinyApp(ui = ui, server = server)

Diff for: rshiny2.R

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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)

Diff for: rshiny3.R

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
library(shiny)
2+
library(dplyr)
3+
library(raster)
4+
5+
# Define the UI
6+
ui <- fluidPage(
7+
titlePanel("Crop and Country Selection"),
8+
sidebarLayout(
9+
sidebarPanel(
10+
selectizeInput("countries", "Add Countries:", choices = c("NGA", "CMR", "TZA", "COD", "AGO", "COG"), multiple = TRUE),
11+
conditionalPanel(
12+
condition = "input.countries.length > 0",
13+
selectizeInput("crop", "Select Crop:", choices = c("WHEA", "RICE", "MAIZ", "BARL", "PMIL", "SMIL"), multiple = TRUE),
14+
selectizeInput("output_type", "Select Output Type:", choices = c("PRODUCTIVITY", "YIELD", "HARV_AREA"), multiple = TRUE)
15+
),
16+
br(),
17+
downloadButton("download_csv", "Download CSV")
18+
),
19+
mainPanel(
20+
dataTableOutput("result_table")
21+
)
22+
)
23+
)
24+
25+
# Define the server logic
26+
server <- function(input, output) {
27+
# Function to extract data for a single country and crop
28+
extractData <- function(country, crop, output_type) {
29+
bd <- raster::getData('GADM', country = country, level = 1)
30+
ad <- length(bd)
31+
32+
thetahat <- vector("numeric", ad)
33+
names1 <- vector("character", ad)
34+
names2 <- vector("character", ad)
35+
36+
x <- ifelse(output_type == "PRODUCTIVITY",
37+
paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_prod.geotiff/spam2010V2r0_global_P_", crop, "_A.tif"),
38+
ifelse(output_type == "YIELD",
39+
paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_yield.geotiff/spam2010V2r0_global_Y_", crop, "_A.tif"),
40+
ifelse(output_type == "HARV_AREA",
41+
paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_harv_area.geotiff/spam2010V2r0_global_H_", crop, "_A.tif"), "")))
42+
43+
raster1 <- raster(here::here(x))
44+
bd_raster <- crop(raster1, extent(bd))
45+
bd_raster2 <- mask(bd_raster, bd)
46+
47+
d <- extract(x = bd_raster2, y = bd, fun = mean, na.rm = TRUE, sp = TRUE)
48+
thetahat <- d@data[, 11] # was 13 before
49+
names1 <- d@data[, 2]
50+
names2 <- d@data[, 4]
51+
52+
data <- data.frame(Country = rep(country, ad),
53+
Crop = rep(crop, ad),
54+
stringsAsFactors = FALSE)
55+
56+
data$District <- names2
57+
data[[output_type]] <- thetahat
58+
59+
data
60+
}
61+
62+
# Create a reactive data frame
63+
result <- reactive({
64+
countries <- input$countries
65+
crops <- input$crop
66+
output_types <- input$output_type
67+
68+
data <- data.frame(Country = character(),
69+
Crop = character(),
70+
stringsAsFactors = FALSE)
71+
72+
for (country in countries) {
73+
for (crop in crops) {
74+
row <- data.frame(Country = country,
75+
Crop = crop,
76+
stringsAsFactors = FALSE)
77+
78+
for (output_type in output_types) {
79+
extracted_data <- extractData(country, crop, output_type)
80+
col_name <- output_type
81+
82+
row <- do.call("rbind", replicate(n=nrow(extracted_data), row, simplify = FALSE)) #added this myself
83+
84+
row <- row[1:nrow(extracted_data),]
85+
86+
row$District <- extracted_data$District
87+
row[[col_name]] <- extracted_data[[col_name]]
88+
89+
row %>% relocate(District, .after=Country)
90+
91+
}
92+
93+
data <- rbind(data, row)
94+
}
95+
}
96+
97+
data
98+
})
99+
100+
# Render the result table
101+
output$result_table <- renderDataTable({
102+
result()
103+
})
104+
105+
# Download the data as a CSV file
106+
output$download_csv <- downloadHandler(
107+
filename = function() {
108+
paste("crop_country_data", Sys.Date(), ".csv", sep = "")
109+
},
110+
content = function(file) {
111+
write.csv(result(), file, row.names = FALSE)
112+
}
113+
)
114+
}
115+
116+
# Run the application
117+
shinyApp(ui = ui, server = server)

0 commit comments

Comments
 (0)