-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessing Public Data.r
86 lines (70 loc) · 2 KB
/
Accessing Public Data.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
#Import libraries
library(jsonlite) # For JSON manipulation
library(ghql) # For working with GraphQL queries
# Function to execute GraphQL query and parse result
execute_query <- function(endpoint, query) {
# Create instance of GraphQL client and set connection settings
con <- GraphqlClient$new(
url = "https://api-ipa.hc-sc.gc.ca/wastewater/"
)
# Execute the query
result <- con$exec(query)
# Parse the result from JSON to a list
results <- fromJSON(result, flatten = TRUE)
# Return the 'data' part of the results
return(results[["data"]])
}
# Define GraphQL queries
# Query for retrieving data from the 'Infobase' table - this table is public
query_infobase <- Query$new()
query_infobase$query('Infobase', '{
Infobase {
Date
Location
region
measureid
fractionid
viral_load
seven_day_rolling_avg
pruid
}
}')
query_WastewaterAtEpiYearWeek <- Query$new()
query_WastewaterAtEpiYearWeek$query('WastewaterAtEpiYearWeek', '{
WastewaterAtEpiYearWeek {
Location
SiteName
City
Province
Country
EpiYear
EpiWeek
Week_start
measure
w_avg
min
max
Population_Coverage
pruid
}
}')
query_InfobaseTrend <- Query$new()
query_InfobaseTrend$query('InfobaseTrend', '{
InfobaseTrend {
Location
measure
latestTrends
pruid
t_low
t_high
LatestLevel
Grouping
City
Province
Country
}
}')
# Call execute_query function to execute GraphQL queries and parse JSON results into data frames
Infobase_df <- execute_query(endpoint, query_infobase$queries$Infobase)[["Infobase"]]
InfobaseTrend_df <- execute_query(endpoint, query_InfobaseTrend$queries$InfobaseTrend)[["InfobaseTrend"]]
WastewaterAtEpiYearWeek_df <- execute_query(endpoint, query_WastewaterAtEpiYearWeek$queries$WastewaterAtEpiYearWeek)[["WastewaterAtEpiYearWeek"]]