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