-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathODmatrix COLDPLAY.R
37 lines (28 loc) · 1.27 KB
/
ODmatrix COLDPLAY.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
library(httr)
library(dplyr)
library(jsonlite)
#parameters
datastreamx.key = "AUdFjYGZd4vwOTzxfPPqA17kzPVYaKFI"
#Show the total number of people on 2017-04-01 ColdPlay Concert who started a trip from National Stadium (Subzone ID: KLSZ05).
#query API
#query API
query.body <- list(
date = "2017-10-29",
timeSeriesReference = "origin",
location = list(locationType = "locationHierarchyLevel", levelType = "origin_subzone", id = "KLSZ05"),
queryGranularity = list(type = "period", period = "P1D"),
aggregations = list(list(metric = "unique_agents", type = "hyperUnique"), list(metric = "total_records", type = "longSum"))
)
query.response <- POST("http://api.datastreamx.com/1925/605/v1/odmatrix/v3/query",
add_headers('DataStreamX-Data-Key' = datastreamx.key),
encode = "json",
body = query.body,
verbose())
stop_for_status(query.response)
cat(content(query.response,as ="text"),"\n")
#convert query response to JSON
data <- fromJSON(rawToChar(query.response$content))
data.df <- do.call(what = "cbind", args = lapply(data, as.data.frame))
names(data.df)[1] = names(data[1])
#write csv to working directory
write.csv(data.df, file = "API-output.csv")