-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_weather_data.R
More file actions
95 lines (72 loc) · 1.86 KB
/
local_weather_data.R
File metadata and controls
95 lines (72 loc) · 1.86 KB
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
#For the creation of the .Renviron file
# install.packages("usethis") # once
# usethis::edit_r_environ() # opens/creates your user .Renviron
key <- Sys.getenv("OPENWEATHER_API_KEY")
key
# should print your key (not empty)
install.packages("jsonlite") # once
library(jsonlite)
city <- "Belmopan,bz" # name,country-code
url <- paste0(
"https://api.openweathermap.org/data/2.5/weather",
"?q=", city,
"&units=imperial",
"&appid=", key
)
j <- fromJSON(url) # open the box (JSON → R list)
# make a tiny table
one_row <- data.frame(
city = j$name,
temp = j$main$temp,
lon = j$coord$lon,
lat = j$coord$lat
)
one_row
#Many cities (baby loop)
cities <- c("Belmopan,bz", "Belize City,bz", "San Ignacio,bz")
all_weather <- data.frame()
for (c in cities) {
u <- paste0("https://api.openweathermap.org/data/2.5/weather",
"?q=", c, "&units=metric&appid=", key)
jj <- fromJSON(u)
row <- data.frame(
city = jj$name,
temp = jj$main$temp,
lon = jj$coord$lon,
lat = jj$coord$lat
)
all_weather <- rbind(all_weather, row) # add the row
}
all_weather
all_weather$hot <- all_weather$temp >= 30 # TRUE if temp ≥ 30
all_weather
#Simplest plot(bar chart)
barplot(
all_weather$temp,
names.arg = all_weather$city,
ylab = "°C",
main = "Current Temperature"
)
#Print png
png("weather_plot.png", width=800, height=600)
barplot(
all_weather$temp,
names.arg = all_weather$city,
ylab = "°C",
main = "Current Temperature"
)
dev.off()
#Simplest map
install.packages("leaflet") # once
library(leaflet)
leaflet(all_weather) |>
addTiles() |>
addCircleMarkers(
lng = ~lon, lat = ~lat,
color = ifelse(all_weather$hot, "red", "blue"),
radius = 6,
label = ~paste(city, "—", temp, "°C")
)
#create csv
dir.create("data", showWarnings = FALSE)
write.csv(all_weather, "data/weather_now.csv", row.names = FALSE)