-
Notifications
You must be signed in to change notification settings - Fork 1
/
food_harvest_establishments_2024.R
56 lines (43 loc) · 1.64 KB
/
food_harvest_establishments_2024.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
library(tidyverse)
library(httr)
library(jsonlite)
## Import data
#Local Authority details (Scotland)
las_scotland <- read_csv("food_scotland_las_2024.csv")
#FHRS API, details of establishments
path <- "http://api.ratings.food.gov.uk/Establishments"
scotland_establishments = data.frame()
#Loop through details of all 32 Local Authorities
for (row in 1:nrow(las_scotland)) {
la_id <- las_scotland [row, "LocalAuthorityId"]
est_count <- las_scotland [row, "EstablishmentCount"]
#Harvest establishments for Local Authority
request <- GET(url = path,
query = list(
localAuthorityId = la_id,
pageNumber = 1,
pageSize = 5000),
add_headers("x-api-version" = "2"))
response <- content(request, as = "text", encoding = "UTF-8") %>%
fromJSON(flatten = TRUE) %>%
pluck("establishments") %>%
as_tibble()
#add to Scotland data frame
scotland_establishments = rbind(scotland_establishments, response)
#Cater for authorities >5000 establishments
if (est_count > 5000) {
request <- GET(url = path,
query = list(
localAuthorityId = la_id,
pageNumber = 2,
pageSize = 5000),
add_headers("x-api-version" = "2"))
response <- content(request, as = "text", encoding = "UTF-8") %>%
fromJSON(flatten = TRUE) %>%
pluck("establishments") %>%
as_tibble()
scotland_establishments = rbind(scotland_establishments, response)
}
}
# Export data
write.csv(scotland_establishments, "food_scotland_establishments_2024.csv", row.names = FALSE)