-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
258 lines (232 loc) · 6.79 KB
/
app.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
library(shiny)
library(bslib)
library(httr)
library(jsonlite)
library(lubridate)
ui <- fluidPage(
div(
class = "intro",
titlePanel("Dashboard - Weather Reports"),
h4(
"Powered by ",
actionLink("website", "edwardcreates.ca", onclick = "window.open('https://www.edwardcreates.ca/', '_blank')")
),
actionButton("github", "", onclick = "window.open('https://github.com/H12M54AM/weather')", icon = icon("github")),
actionButton(
"linkedin",
"",
onclick = "window.open('https://www.linkedin.com/in/edward-naidoo-595ab5224/')",
icon = icon("linkedin")
),
actionButton("devto", "", onclick = "window.open('https://dev.to/edward_naidoo')", icon = icon("dev")),
),
sidebarLayout(
sidebarPanel(
card(
actionButton("van", "Vancouver", icon = icon("building")),
actionButton("bur", "Burnaby", icon = icon("building")),
actionButton("rich", "Richmond", icon = icon("building")),
actionButton("sur", "Surrey", icon = icon("building")),
actionButton("lang", "Langley", icon = icon("building")),
actionButton("abby", "Abbotsford", icon = icon("building")),
),
),
mainPanel(
actionButton("refresh", "Refresh", icon = icon("arrows-rotate")),
card(
div(
class = "k-hstack",
layout_columns(
value_box(
title = "Current Temp - Comming Soon...",
value = "XX ˚C",
showcase = bsicons::bs_icon("calendar-event"),
theme = value_box_theme(bg = "#e6f2fd", fg = "#0B538E"),
class = "value_box"
),
value_box(
title = "Lowest Temperature",
value = textOutput("minTemp"),
showcase = bsicons::bs_icon("thermometer-low"),
theme = value_box_theme(bg = "#e6f2fd", fg = "#0B538E"),
class = "value_box"
),
value_box(
title = "Highest Temperature",
value = textOutput("maxTemp"),
showcase = bsicons::bs_icon("thermometer-high"),
theme = value_box_theme(bg = "#e6f2fd", fg = "#0B538E"),
class = "value_box"
),
value_box(
title = "Average Temperature",
value = textOutput("avgTemp"),
showcase = bsicons::bs_icon("calendar3"),
theme = value_box_theme(bg = "#e6f2fd", fg = "#0B538E"),
class = "value_box"
),
),
),
tags$style(
"
.k-hstack {
margin-top: 1rem;
margin-bottom: 3rem;
}
.intro {
margin-bottom: 4rem;
}
.value_box {
width: 4/6;
}
"
),
),
card(plotOutput("apiPlot")),
card(plotOutput("statPlot")),
card(plotOutput("boxPlot"))
)
)
)
server <- function(input, output) {
observeEvent(input$refresh, {
# Refresh the entire page
session$reload()
})
# This function is a template used
# to allow users to click on any
# button and recieve different
# data in that city.
# For example; A user can click
# the 'Vancouver' button and
# recieve weather data from
# vancity. If a user want to see
# Surrey, they can click Surrey.
dataDisplay <- function(api) {
return(time)
}
# Must pull weather data from Vancouver
fetchData <- function() {
response <- GET(
"https://api.open-meteo.com/v1/forecast?latitude=49.2497&longitude=-123.1193&hourly=temperature_2m&timeformat=unixtime&timezone=America%2FLos_Angeles&forecast_days=1"
)
if (status_code(response) >= 200 &&
status_code(response) < 400) {
data <- fromJSON(content(response, "text"))
return(list(
time = data$hourly$time,
temp = data$hourly$temperature_2m
))
}
if (status_code(response) >= 400 &&
status_code(response) < 500) {
return("API Failed due to Human Error")
}
if (status_code(response) >= 500 &&
status_code(response) < 600) {
return("API Failed due to Server Side Error\n\nPlease review the server...")
}
}
# Min Temperature today
output$minTemp <- renderText({
data <- fetchData()
paste(round(min(data$temp, na.rm = TRUE), digits = 2), "°C")
})
# Max Temperature today
output$maxTemp <- renderText({
data <- fetchData()
paste(round(max(data$temp, na.rm = TRUE), digits = 2), "°C")
})
# Avg Temperature today
output$avgTemp <- renderText({
data <- fetchData()
paste(round(mean(data$temp, na.rm = TRUE), digits = 2), "°C")
})
# Line Chart of Hourly Changes in Temperature
output$apiPlot <- renderPlot({
data <- fetchData()
chartTitle = "Todays Temperature in Vancouver per Hour"
# Convert Unix timestamp to R-readable timestamp
r_timestamp <- as.Date(as.POSIXct(as.numeric(data$time), origin = "1970-01-01"))
# Used to show what the datetime info looks like before using the as.Date function
unconverted <- as.POSIXct(as.numeric(data$time), origin = "1970-01-01")
# Isolates the Time from the Datetime
unconverted_hours <- format(unconverted, "%H:%M")
listOfHours <- c(
"00:00",
"01:00",
"02:00",
"03:00",
"04:00",
"05:00",
"06:00",
"07:00",
"08:00",
"09:00",
"10:00",
"11:00",
"12:00",
"13:00",
"14:00",
"15:00",
"16:00",
"17:00",
"18:00",
"19:00",
"20:00",
"21:00",
"22:00",
"23:00"
)
xLimits <- 1:length(listOfHours)
plot(
xLimits,
data$temp,
type = "b",
col = "#0B538E",
xlab = "Hours in PST",
ylab = "Temperature (°C)",
main = chartTitle,
xlim = range(xLimits)
)
text(
xLimits,
data$temp,
labels = as.character(data$temp),
pos = 1,
offset = 1.1
)
})
# Box Plot of the Temp Data
output$boxPlot <- renderPlot({
data <- fetchData()
boxplot(data$temp, main = "Boxplot of Todays Changing Weather", col = "#e6f2fd")
})
# Bar Chart of general Stats
output$statPlot <- renderPlot({
data <- fetchData()
chartTitle = "Comparison of General Stats about Hourly Temps"
yaxis = c(
min(data$temp, na.rm = TRUE),
mean(data$temp, na.rm = TRUE),
IQR(data$temp, na.rm = TRUE),
max(data$temp, na.rm = TRUE)
)
xLabels = c("Min Temp", "Average Temp", "IQR", "Max Temp")
barplot(
yaxis,
xlab = "Stats",
ylab = "Temperature ˚C",
names.arg = xLabels,
col = "#e6f2fd",
main = chartTitle
)
text(
x = xLabels,
labels = yaxis,
pos = 3,
offset = yaxis + 1
)
})
}
shinyApp(ui = ui, server = server)