-
Notifications
You must be signed in to change notification settings - Fork 0
/
rshiny.R
85 lines (62 loc) · 2.4 KB
/
rshiny.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
library(shiny)
library(readr)
library(raster)
# Define the UI
ui <- fluidPage(
titlePanel("Crop and Country Selection"),
sidebarLayout(
sidebarPanel(
selectInput("crop", "Select Crop:", choices = c("WHEA", "RICE")),
selectInput("country", "Select Country:", choices = c("NGA", "CMR")),
selectInput("agricultural_output", "Select Output Type:", choices = c("PRODUCTIVITY", "YIELD")),
br(),
downloadButton("download_csv", "Download CSV")
),
mainPanel(
textOutput("selected_options")
)
)
)
# Define the server logic
server <- function(input, output) {
# Display selected options
output$selected_options <- renderText({
paste("Selected Crop:", input$crop, "\n",
"Selected Country:", input$country, "\n",
"Selected Output Type:", input$agricultural_output)
})
# Create a reactive data frame
data <- reactive({
bd <- raster::getData('GADM', country=input$country, level=1)
ad <- length(bd)
thetahat=matrix(NA,ad,1)
names=matrix(NA,ad,2)
#x <- paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_prod.geotiff/spam2010V2r0_global_P_", input$crop, "_A.tif")
x <- ifelse(input$agricultural_output == "PRODUCTIVITY", paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_prod.geotiff/spam2010V2r0_global_P_", input$crop, "_A.tif"),
ifelse(input$agricultural_output == "YIELD", paste0("C:/Users/syedm/Desktop/soil/spam2010v2r0_global_yield.geotiff/spam2010V2r0_global_Y_", input$crop, "_A.tif"), ""))
raster1 <- raster(here::here(x)) # load file
# apply function
bd_raster <- crop(raster1, extent(bd))
#plot(bd_raster)
bd_raster2 <- mask(bd_raster, bd)
#plot(bd_raster2)
#plot(bd, add=TRUE)
d <- extract(x=bd_raster2, y=bd, fun=mean, na.rm=TRUE, sp=T)
thetahat[,1]=d@data[,11] #was 13 before
names[,1]=d@data[,2]
names[,2]=d@data[,4]
mat <- cbind(names, thetahat)
data <- as.data.frame(mat)
})
# 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(data(), file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)