-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_fertiliser_usage.R
128 lines (113 loc) · 3.76 KB
/
module_fertiliser_usage.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
# File: module_fertiliser_usage.R
fertiliserUsageUI <- function(id) {
ns <- NS(id)
tagList(
sidebarLayout(
sidebarPanel(
width = 3,
radioButtons(ns("data_type"), "Data Type", choices = c("Holdings" = "holdings", "Area" = "area"), selected = "holdings")
),
mainPanel(
id = ns("mainpanel"),
width = 9,
tabsetPanel(
id = ns("tabs"),
tabPanel("Bar Chart", barChartUI(ns("bar_chart")), value = ns("bar")),
tabPanel("Data Table",
DTOutput(ns("data_table")),
tags$div(
style = "margin-top: 20px;",
downloadButton(ns("downloadData"), "Download Data"),
generate2023ModuleTableFooter()
),
value = ns("data"))
)
)
)
)
}
fertiliserUsageServer <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
# Data for the chart (keeps numeric values unformatted)
chart_data <- reactive({
data <- manure_fertiliser
if (!is.null(input$variables)) {
data <- data %>%
filter(`Fertiliser by type` %in% input$variables)
}
data
})
# Data for the table (formats numeric values with commas)
table_data <- reactive({
chart_data() %>%
mutate(across(where(is.numeric), comma)) %>%
rename(Holdings = `2023 holdings (Number)`,
Area = `2023 area (Hectares)`)
})
output$variable_select <- renderUI({
choices <- unique(manure_fertiliser$`Fertiliser by type`)
selected <- choices
checkboxGroupInput(ns("variables"), "Choose variables to add to chart", choices = choices, selected = selected)
})
observeEvent(input$select_all_button, {
updateCheckboxGroupInput(session, ns("variables"), selected = unique(manure_fertiliser$`Fertiliser by type`))
})
observeEvent(input$deselect_all_button, {
updateCheckboxGroupInput(session, ns("variables"), selected = character(0))
})
y_col <- reactive({
if (input$data_type == "holdings") {
"2023 holdings (Number)"
} else {
"2023 area (Hectares)"
}
})
yAxisTitle <- reactive({
if (input$data_type == "holdings") {
"Holdings"
} else {
"Area (1,000 hectares)"
}
})
tooltip_unit <- reactive({
if (input$data_type == "holdings") {
"holdings"
} else {
"hectares"
}
})
# Bar chart rendering using unformatted data
barChartServer(
id = "bar_chart",
chart_data = chart_data,
title = "Fertiliser Usage by Type in Scotland",
yAxisTitle = yAxisTitle,
xAxisTitle = "Fertiliser Type",
unit = "",
footer = '<div style="font-size: 16px; font-weight: bold;"><a href="https://www.gov.scot/publications/results-from-the-scottish-agricultural-census-module-june-2023/" target="_blank">Source: Scottish Agricultural Census: Module June 2023</a></div>',
x_col = "Fertiliser by type",
y_col = y_col,
tooltip_unit = tooltip_unit
)
# Data table rendering using formatted data
output$data_table <- renderDT({
datatable(
table_data(),
options = list(
scrollX = TRUE, # Enable horizontal scrolling
pageLength = 20 # Show 20 entries by default
)
)
})
# Download handler for the data table with appropriate naming
output$downloadData <- downloadHandler(
filename = function() {
paste("Fertiliser_Usage_Data_", Sys.Date(), ".csv", sep = "")
},
content = function(file) {
write.csv(table_data(), file, row.names = FALSE)
}
)
})
}