-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_potatoes.R
174 lines (163 loc) · 5.34 KB
/
module_potatoes.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File: module_potatoes.R
potatoesUI <- function(id) {
ns <- NS(id)
sidebarLayout(
sidebarPanel(
width = 3,
conditionalPanel(
condition = "input.tabsetPanel === 'Map'",
ns = ns,
radioButtons(
ns("variable"),
"Select Variable",
choices = unique(potatoes_subregion$`Land use by category`)
)
),
conditionalPanel(
condition = "input.tabsetPanel === 'Time Series' || input.tabsetPanel === 'Area Chart'",
ns = ns,
checkboxGroupInput(
ns("timeseries_variables"),
"Select Time Series Variables",
choices = unique(potatoes_data$`Crop/Land use`),
selected = c(
"Ware potatoes",
"Seed potatoes"
)
)
),
conditionalPanel(
condition = "input.tabsetPanel === 'Data Table'",
ns = ns,
radioButtons(
ns("table_data"),
"Select Data to Display",
choices = c("Map Data" = "map", "Time Series Data" = "timeseries"),
selected = "map"
)
)
),
mainPanel(
width = 9,
tabsetPanel(
id = ns("tabsetPanel"),
tabPanel("Map", mapUI(ns("map"))),
tabPanel("Time Series", lineChartUI(ns("line"))),
tabPanel("Area Chart", areaChartUI(ns("area"))),
tabPanel("Data Table",
DTOutput(ns("table")),
downloadButton(ns("download_data"), "Download Data"),
generateCensusTableFooter()
)
)
)
)
}
potatoesServer <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
potatoes_map <- potatoes_subregion %>%
select(-`Scotland total`) %>%
mutate(across(everything(), as.character)) %>%
pivot_longer(cols = -`Land use by category`, names_to = "sub_region", values_to = "value") %>%
mutate(value = safe_as_numeric(value))
mapServer(
id = "map",
data = reactive({
req(input$variable)
potatoes_map %>% filter(`Land use by category` == input$variable)
}),
unit = "hectares",
footer = census_footer,
variable = reactive(input$variable),
title = paste("Potatoes distribution by region in Scotland in", census_year),
legend_title = "Area (hectares)"
)
chart_data <- reactive({
req(input$timeseries_variables)
filtered_data <- potatoes_data %>%
filter(`Crop/Land use` %in% input$timeseries_variables) %>%
pivot_longer(cols = -`Crop/Land use`, names_to = "year", values_to = "value") %>%
mutate(year = as.numeric(year)) # Ensure year is numeric
filtered_data
})
areaChartServer(
id = "area",
chart_data = chart_data,
title = "Area used to grow potatoes over time",
yAxisTitle = "Area of potatoes (1,000 hectares)",
xAxisTitle = "Year",
unit = "hectares",
footer = census_footer,
x_col = "year",
y_col = "value"
)
lineChartServer(
id = "line",
chart_data = chart_data,
title = "Area used to grow potatoes over time",
yAxisTitle = "Area of potatoes (1,000 hectares)",
xAxisTitle = "Year",
unit = "hectares",
footer = census_footer,
x_col = "year",
y_col = "value"
)
output$table <- renderDT({
req(input$tabsetPanel == "Data Table")
if (input$table_data == "map") {
req(input$variable)
potatoes_map %>%
filter(`Land use by category` == input$variable) %>%
pivot_wider(names_from = sub_region, values_from = value) %>%
mutate(across(where(is.numeric) & !contains("Year"), comma)) %>%
datatable(
options = list(
scrollX = TRUE, # Enable horizontal scrolling
pageLength = 20 # Show 20 entries by default
)
)
} else {
potatoes_data %>%
pivot_longer(cols = -`Crop/Land use`, names_to = "year", values_to = "value") %>%
pivot_wider(names_from = year, values_from = value) %>%
mutate(across(where(is.numeric) & !contains("Year"), comma))%>%
datatable(
options = list(
scrollX = TRUE, # Enable horizontal scrolling
pageLength = 20 # Show 20 entries by default
)
)
}
})
output$download_data <- downloadHandler(
filename = function() {
if (input$table_data == "map") {
paste("Potato_Subregion_Data_", Sys.Date(), ".csv", sep = "")
} else {
paste("Potato_Timeseries_Data_", Sys.Date(), ".csv", sep = "")
}
},
content = function(file) {
data <- if (input$table_data == "map") {
potatoes_map %>%
filter(`Land use by category` == input$variable) %>%
pivot_wider(names_from = sub_region, values_from = value)
} else {
potatoes_data %>%
pivot_longer(cols = -`Crop/Land use`, names_to = "year", values_to = "value") %>%
pivot_wider(names_from = year, values_from = value)
}
write.csv(data, file, row.names = FALSE)
}
)
})
}
potatoes_demo <- function() {
ui <- fluidPage(potatoesUI("potatoes_test"))
server <- function(input, output, session) {
potatoesServer("potatoes_test")
}
shinyApp(ui, server)
}
potatoes_demo()