-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_pigs.R
188 lines (176 loc) · 5.83 KB
/
module_pigs.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
# File: module_pigs.R
pigsUI <- function(id) {
ns <- NS(id)
sidebarLayout(
sidebarPanel(
width = 3,
conditionalPanel(
condition = "input.tabsetPanel === 'Map'",
ns = ns,
radioButtons(
ns("variable"),
"Select Variable",
choices = c(
"Total pigs" = "Total pigs",
"Female pigs breeding herd" = "Female pigs breeding herd",
"All other non-breeding pigs" = "All other non-breeding pigs"
))
),
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_pigs$`Pigs by category`),
selected = c(
"Total breeding herd",
"80 kg liveweight and over",
"50 kg and under 80 kg liveweight",
"Under 50 kg liveweight"
),
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()
)
)
)
)
}
pigsServer <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
pigs_data <- livestock_subregion %>%
filter(`Livestock by category` %in% c(
"Female pigs breeding herd",
"All other non-breeding pigs",
"Total pigs"
)) %>%
select(-Scotland) %>%
mutate(across(everything(), as.character)) %>%
pivot_longer(cols = -`Livestock by category`, names_to = "sub_region", values_to = "value") %>%
mutate(value = as.numeric(value)) # Ensure value is numeric
mapServer(
id = "map",
data = reactive({
req(input$variable)
pigs_data %>% filter(`Livestock by category` == input$variable)
}),
footer = census_footer,
variable = reactive(input$variable),
title = paste("Pig distribution by region in Scotland in", census_year),
legend_title = "Number of pigs"
)
chart_data <- reactive({
req(input$timeseries_variables)
filtered_data <- number_of_pigs %>%
mutate(across(-`Pigs by category`, as.numeric)) %>%
filter(`Pigs by category` %in% input$timeseries_variables) %>%
pivot_longer(cols = -`Pigs by category`, names_to = "year", values_to = "value") %>%
mutate(value = as.numeric(value)) # Ensure value is numeric
filtered_data
})
areaChartServer(
id = "area",
chart_data = chart_data,
title = "Number of pigs by category over time",
yAxisTitle = "Number of pigs (1,000)",
xAxisTitle = "Year",
footer = census_footer,
x_col = "year",
y_col = "value"
)
lineChartServer(
id = "line",
chart_data = chart_data,
title = "Number of pigs by category over time",
yAxisTitle = "Number of pigs (1,000)",
xAxisTitle = "Year",
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)
pigs_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_pigs %>%
pivot_longer(cols = -`Pigs 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("Pigs_Map_Data_", Sys.Date(), ".csv", sep = "")
} else {
paste("Pigs_Timeseries_Data_", Sys.Date(), ".csv", sep = "")
}
},
content = function(file) {
data <- if (input$table_data == "map") {
pigs_data %>%
filter(`Livestock by category` == input$variable) %>%
pivot_wider(names_from = sub_region, values_from = value)
} else {
number_of_pigs %>%
pivot_longer(cols = -`Pigs 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
pigs_demo <- function() {
ui <- fluidPage(pigsUI("pigs_test"))
server <- function(input, output, session) {
pigsServer("pigs_test")
}
shinyApp(ui, server)
}
pigs_demo()