-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample2022_maps.R
154 lines (143 loc) · 6.9 KB
/
sample2022_maps.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
# Install packages if not already installed
mypackages <- c("shiny",
"shinythemes",
"sf",
"mapview",
"leaflet",
"leafpop")
for (p in mypackages){
if(!require(p, character.only = TRUE)){
install.packages(p)
library(p, character.only = TRUE)
}
}
# Load packages
library(shiny)
library(shinythemes)
library(sf)
library(mapview)
library(leaflet)
library(leafpop)
# Load data
load("sample_LUCAS.RData")
samptot <- samptot[order(paste0(samptot$NUTS0,samptot$NUTS2)),]
samp_sf <- st_as_sf(samptot, coords = c("LON", "LAT"),
crs=" +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs ")
samp_sf$LC <- as.factor(samp_sf$LC)
levels(samp_sf$LC) <- LETTERS[1:8]
ui <- fluidPage(theme = shinytheme("lumen"),
titlePanel("LUCAS 2022 Sample"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "background", label = strong("Maps background"),
choices = c("OpenStreetMap",
"Esri.WorldImagery",
"OpenTopoMap"),
selected = "OpenStreetMap"),
# Select countries and regions
selectInput(inputId = "Country", label = strong("Country (NUTS0)"),
choices = levels(samp_sf$NUTS0),
selected = "AT"),
selectInput(inputId = "Region", label = strong("Region (NUTS2)"),
choices = unique(samp_sf$NUTS2),
selected = "AT11"),
selectInput(inputId = "ObsType", label = strong("Observation type"),
choices = c("All values"="all",
"Field"="FI",
"Photo-Interpreted"="PI"),
selected = ""),
selectInput(inputId = "var", label = strong("Variable of interest"),
choices = c("Land cover"="LC",
"Land use"="LU"),
selected = "LC"),
# Only show this panel if the variable is Land Cover
conditionalPanel(
condition = "input.var == 'LC'",
selectInput(inputId = "value", label = "Select values of Land Cover:",
choices = c("All values"="all",
"Artificial"="A",
"Cropland"="B",
"Woodland"="C",
"Shrubland"="D",
"Grassland"="E",
"Bareland"="F",
"Water"="G",
"Wetland"="H"),
selected = "")
),
# Only show this panel if the variable is Land Use
conditionalPanel(
condition = "input.var == 'LU'",
selectInput(inputId = "value", label = "Select values of Land Use:",
choices = c("All values"="all",
"Primary Sector"="U1",
"Secondary Sector"="U2",
"Tertiary Sector"="U3",
"Abandoned or unused areas"="U4"),
selected = "")
),
actionButton("do", "Click button"),
width = 3),
# Output
mainPanel(leafletOutput('map', width = "100%", height = 700))
)
)
server <- function(input, output, session) {
observeEvent(input$do, {
# Subset data
selected_sample <- reactive({
req(input$Country)
validate(need(!is.na(input$Country) & (input$Country %in% levels(as.factor(samp_sf$NUTS0))), "Error: Please provide a valid country code"))
req(input$Region)
validate(need(!is.na(input$Region) & (input$Region %in% levels(as.factor(samp_sf$NUTS2))), "Error: Please provide valid region code"))
req(input$ObsType)
validate(need(!is.na(input$ObsType), "Error: Please provide valid observation type code"))
req(input$var)
validate(need(!is.na(input$var), "Error: Please provide valid variable name"))
req(input$value)
validate(need(!is.na(input$var), "Error: Please provide valid value"))
samp <- samp_sf
if (input$ObsType == "all")
if (input$value == "all")
samp[samp$NUTS0 == input$Country & samp$NUTS2 == input$Region,]
else
if (input$var == "LC")
samp[samp$NUTS0 == input$Country & samp$NUTS2 == input$Region & samp$LC == input$value, ]
else
samp[samp$NUTS0 == input$Country & samp$NUTS2 == input$Region & samp$LU == input$value, ]
else
if (input$value == "all")
samp[samp$PI == input$ObsType & samp$NUTS0 == input$Country & samp$NUTS2 == input$Region, ]
else
if (input$var == "LC")
samp[samp$PI == input$ObsType & samp$NUTS0 == input$Country & samp$NUTS2 == input$Region & samp$LC == input$value, ]
# else
# samp[samp$PI == input$ObsType & samp$NUTS0 == input$Country & samp$NUTS2 == input$Region & samp$LU == input$value, ]
})
# Pull the map
output$map <- renderLeaflet({
if (input$background == "OpenStreetMap")
if (input$var == "LC")
mapView(selected_sample()["LC"], map.types = c("OpenStreetMap"))@map
else
mapView(selected_sample()["LU"], map.types = c("OpenStreetMap"))@map
else
if (input$background == "Esri.WorldImagery")
if (input$var == "LC")
mapView(selected_sample()["LC"], map.types = c("Esri.WorldImagery"))@map
else
mapView(selected_sample()["LU"], map.types = c("Esri.WorldImagery"))@map
else
if (input$background == "OpenTopoMap")
if (input$var == "LC")
mapView(selected_sample()["LC"], map.types = c("OpenTopoMap"))@map
else
mapView(selected_sample()["LU"], map.types = c("OpenTopoMap"))@map
})
# output$desc <- renderText({
# paste(trend_text, "The index is set to 1.0 on January 1, 2004 and is calculated only for US search traffic.")
# })
})
}
# Create Shiny object
shinyApp(ui, server)