-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_poultry.R
192 lines (181 loc) · 6.79 KB
/
module_poultry.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File: module_poultry.R
poultryUI <- function(id) {
ns <- NS(id)
sidebarLayout(
sidebarPanel(
width = 3,
conditionalPanel(
condition = "input.tabsetPanel === 'Map'",
ns = ns,
radioButtons(
ns("variable"),
"Select Variable",
choices = c(
"Total Poultry" = "Total Poultry",
"Fowls for producing eggs" = "Fowls for producing eggs",
"Fowls for breeding" = "Fowls for breeding",
"Broilers and other table fowls and other poultry" = "Broilers and other table fowls and other poultry"
)
)
),
conditionalPanel(
condition = "input.tabsetPanel === 'Time Series' || input.tabsetPanel === 'Area Chart'",
ns = ns,
selectizeInput(
ns("timeseries_variables"),
"Click within the box to select variables",
choices = unique(number_of_poultry$`Poultry by category`),
selected = c(
"Total fowls for producing eggs",
"Total fowls for breeding",
"Broilers and other table birds",
"Total Poultry"
),
multiple = TRUE,
options = list(
plugins = list('remove_button')
)
)
),
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"), note_type = 2)),
tabPanel("Area Chart", areaChartUI(ns("area"), note_type = 2)),
tabPanel("Data Table",
DTOutput(ns("table")),
downloadButton(ns("downloadData"), "Download Data"),
generateCensusTableFooter()
)
),
div(
style = "margin-top: 20px; padding: 10px; border-top: 1px solid #ddd;",
HTML("<strong>Note:</strong> Poultry estimates for 2023 are not comparable to previous years due to methodological improvements.")
)
)
)
}
poultryServer <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
poultry_data <- livestock_subregion %>%
filter(`Livestock by category` %in% c(
"Fowls for producing eggs",
"Fowls for breeding",
"Broilers and other table fowls and other poultry",
"Total Poultry"
)) %>%
select(-`Scotland total`) %>%
mutate(across(everything(), as.character)) %>%
pivot_longer(cols = -`Livestock by category`, names_to = "sub_region", values_to = "value") %>%
mutate(value = safe_as_numeric(value))
mapServer(
id = "map",
data = reactive({
req(input$variable)
poultry_data %>% filter(`Livestock by category` == input$variable)
}),
footer = census_footer,
variable = reactive(input$variable),
title = paste("Poultry distribution by region in Scotland in", census_year),
legend_title = "Number of poultry"
)
chart_data <- reactive({
req(input$timeseries_variables)
filtered_data <- number_of_poultry %>%
filter(`Poultry by category` %in% input$timeseries_variables) %>%
pivot_longer(cols = -`Poultry by category`, names_to = "year", values_to = "value") %>%
mutate(value = safe_as_numeric(value))
})
areaChartServer(
id = "area",
chart_data = chart_data,
title = "Number of poultry by category across time",
yAxisTitle = "Number of poultry (1,000)",
xAxisTitle = "Year",
footer = '<div style="font-size: 16px; font-weight: bold;">* Estimates for 2023 are not comparable to previous years due to methodological improvements.<br/><a href="https://www.gov.scot/publications/results-scottish-agricultural-census-june-2023/documents/">Source: Scottish Agricultural Census: June 2023</a></div>',
x_col = "year",
y_col = "value"
)
lineChartServer(
id = "line",
chart_data = chart_data,
title = "Number of poultry by category across time",
yAxisTitle = "Number of poultry (1,000)",
xAxisTitle = "Year",
footer = '<div style="font-size: 16px; font-weight: bold;">* Estimates for 2023 are not comparable to previous years due to methodological improvements.<br/><a href="https://www.gov.scot/publications/results-scottish-agricultural-census-june-2023/documents/">Source: Scottish Agricultural Census: June 2023</a></div>',
x_col = "year",
y_col = "value"
)
output$table <- renderDT({
req(input$tabsetPanel == "Data Table")
if (input$table_data == "map") {
req(input$variable)
poultry_data %>%
filter(`Livestock 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 {
number_of_poultry %>%
pivot_longer(cols = -`Poultry by category`, 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$downloadData <- downloadHandler(
filename = function() {
if (input$table_data == "map") {
paste("Poultry_Map_Data_", Sys.Date(), ".csv", sep = "")
} else {
paste("Poultry_Timeseries_Data_", Sys.Date(), ".csv", sep = "")
}
},
content = function(file) {
data <- if (input$table_data == "map") {
poultry_data %>%
filter(`Livestock by category` == input$variable) %>%
pivot_wider(names_from = sub_region, values_from = value)
} else {
number_of_poultry %>%
pivot_longer(cols = -`Poultry by category`, names_to = "year", values_to = "value") %>%
pivot_wider(names_from = year, values_from = value)
}
write.csv(data, file, row.names = FALSE)
}
)
})
}
# Testing module
poultry_demo <- function() {
ui <- fluidPage(poultryUI("poultry_test"))
server <- function(input, output, session) {
poultryServer("poultry_test")
}
shinyApp(ui, server)
}
poultry_demo()