-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
134 lines (116 loc) · 4.74 KB
/
server.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
server <- function(input, output, session) {
# Re-actively filter data based on the selected country and year
selected_data <- reactive({
# Require country and year inputs
req(input$country, input$year)
filtered_data <- cereal_data %>%
filter(
entity == input$country,
year >= input$year[1] & year <= input$year[2]
)
return(filtered_data)
})
output$plot <- renderEcharts4r({
# Reactive data
selected <- selected_data()
if (nrow(selected) == 0) {
shinyalert(title = "Data Not Available",
text = "Data is not available or contains missing values for the selected country and year.",
type = "error",
animation = TRUE,
confirmButtonCol = "#78C2AD",
confirmButtonText = "BACK",)
return()
}
# Checking for missing values
if (any(is.na(selected$area_harvested_hectares)) ||
any(is.na(selected$production_tonnes)) ||
any(is.na(selected$yield_tonnes_per_hectare))) {
shinyalert(
title = "Data Not Available",
text = "Data is not available or contains missing values for the selected country and year.",
type = "error",
animation = TRUE,
confirmButtonCol = "#78C2AD",
confirmButtonText = "BACK",)
return()
}
# Checking if data for the initial year is available
if (sum(selected$year == input$year[1]) == 0) {
# if data for the initial year is not available for the selected country
# then use the next available year's data for calculations
initial_year_data <- selected %>% filter(year == min(selected$year))
} else {
initial_year_data <- selected %>% filter(year == input$year[1])
}
# Calculating %change in Area Harvested
change_area_harvested <- selected %>%
mutate(Area_Harvested = round(((area_harvested_hectares - initial_year_data$area_harvested_hectares) / initial_year_data$area_harvested_hectares) * 100, 1))
# Calculating %change in Production
change_production <- selected %>%
mutate(Production = round(((production_tonnes - initial_year_data$production_tonnes) / initial_year_data$production_tonnes) * 100, 1))
# Calculating %change in Yield
change_yield <- selected %>%
mutate(Yield = round(((yield_tonnes_per_hectare - initial_year_data$yield_tonnes_per_hectare) / initial_year_data$yield_tonnes_per_hectare) * 100, 1))
# Change data type of Year columns
change_area_harvested$year <- as.character(change_area_harvested$year)
change_production$year <- as.character(change_production$year)
change_yield$year <- as.character(change_yield$year)
# Combined line plot for % change in (Yield, Production, and Area-Harvested)
change_area_harvested %>%
e_charts(year) %>%
e_line(Area_Harvested) %>%
e_data(change_production) %>%
e_line(serie = Production) %>%
e_data(change_yield) %>%
e_line(serie = Yield) %>%
e_axis_labels(y = "[Change in %]") %>%
e_tooltip(trigger = "item") %>%
e_legend(bottom = 0) %>%
e_toolbox_feature(feature = "dataView")
})
output$plot_title <- renderText({
# Selected country and year range
selected_country <- input$country
selected_year_range <- paste(input$year[1], "-", input$year[2])
# Dynamic title
title <- paste("% Change of Cereal Production in ", selected_country, " since ", selected_year_range)
return(title)
})
output$total_area_harvested <- renderText({
# Reactive data
selected <- selected_data()
paste(round(sum(selected$area_harvested_hectares, na.rm = TRUE)/1000000, 2), "Millions")
})
output$total_production <- renderText({
# Reactive data
selected <- selected_data()
paste(round(sum(selected$production_tonnes, na.rm = TRUE)/1000000, 2), "Millions")
})
output$total_yeild <- renderText({
# Reactive data
selected <- selected_data()
paste(round(sum(selected$yield_tonnes_per_hectare, na.rm = TRUE), 2), "Millions")
})
output$left_box_title <- renderText({
# Selected country and year range
selected_country <- input$country
# Dynamic title
title <- paste("Total Area Harvested in ", selected_country, "(Hectares)")
return(title)
})
output$middle_box_title <- renderText({
# Selected country and year range
selected_country <- input$country
# Dynamic title
title <- paste("Total Cereal Production in ", selected_country, "(Tonnes)")
return(title)
})
output$right_box_title <- renderText({
# Selected country and year range
selected_country <- input$country
# Dynamic title
title <- paste("Total Cereal Yeild in ", selected_country, "(Tonnes)")
return(title)
})
}