-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueryAPI.R
More file actions
191 lines (154 loc) · 6.53 KB
/
Copy pathqueryAPI.R
File metadata and controls
191 lines (154 loc) · 6.53 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
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
library(XML)
prefix = "http://www.whatwepayfor.com/api/"
getBudgetAccount <- function(year="", income="", exacttax="", fn="", subfn="", agency="", bureau="", account="", adjInflYear="", type="", sortby="", sortdir="", filing="", selfEmployed="", showExtra="", showChange="") {
# Test usage of this function:
# test = getBudgetAccount(year=2009, income=50000, agency=3815)
# Generate the API query:
query = paste(prefix,"getBudgetAccount?",sep="")
params <- list("year"=year,"income"=income,"exacttax"=exacttax,"function"=fn,
"subfunction"=subfn,"agency"=agency,"bureau"=bureau,"account"=account,
"adjustInflationYear"=adjInflYear,"type"=type,"sortby"=sortby,
"sortdir"=sortdir,"filing"=filing,"selfEmployed"=selfEmployed,
"showExtra"=showExtra,"showChange"=showChange)
arguments = pasteArgs(params)
query = paste(query, arguments, sep="")
# Get the result of the query
xmlResult <- xmlRoot(xmlTreeParse(query))
# Get attribute values from the api call
result <- xmlToDataframe(xmlResult)
return(result)
}
getBudgetAggregate <- function(year="", income="", exacttax="", fn="", subfn="", agency="", bureau="", account="", adjInflYear="", type="", sortby="", sortdir="", filing="", selfEmployed="", showExtra="", showChange="", group="") {
# Test usage of this function:
# test = getBudgetAggregate(year=2009, income=50000, agency=3815)
# Generate the API query:
query = paste(prefix,"getBudgetAggregate?",sep="")
params <- list("year"=year,"income"=income,"exacttax"=exacttax,"function"=fn,
"subfunction"=subfn,"agency"=agency,"bureau"=bureau,"account"=account,
"adjustInflationYear"=adjInflYear,"type"=type,"sortby"=sortby,
"sortdir"=sortdir,"filing"=filing,"selfEmployed"=selfEmployed,
"showExtra"=showExtra,"showChange"=showChange, "group"=group)
arguments = pasteArgs(params)
query = paste(query, arguments, sep="")
# Get the result of the query
xmlResult <- xmlRoot(xmlTreeParse(query))
# Get attribute values from the api call
result <- xmlToDataframe(xmlResult)
return(result)
}
getReceiptAccount <- function(year="", exacttax="", categ="", subcateg="", agency="", bureau="", account="", adjInflYear="", type="", sortby="", sortdir="", filing="", selfEmployed="", showExtra="", showChange="") {
# Test usage of this function:
# test = getBudgetAccount(year=2009, income=50000, agency=3815)
# Generate the API query:
query = paste(prefix,"getReceiptAccount?",sep="")
params <- list("year"=year,"exacttax"=exacttax,"category"=categ,
"subcategory"=subcateg,"agency"=agency,"bureau"=bureau,"account"=account,
"adjustInflationYear"=adjInflYear,"type"=type,"sortby"=sortby,
"sortdir"=sortdir,"filing"=filing,"selfEmployed"=selfEmployed,
"showExtra"=showExtra,"showChange"=showChange)
arguments = pasteArgs(params)
query = paste(query, arguments, sep="")
# Get the result of the query
xmlResult <- xmlRoot(xmlTreeParse(query))
# Get attribute values from the api call
result <- xmlToDataframe(xmlResult)
return(result)
}
getReceiptAggregate <- function(year="", exacttax="", categ="", subcateg="", agency="", bureau="", account="", adjInflYear="", type="", sortby="", sortdir="", filing="", selfEmployed="", showExtra="", showChange="", group="") {
# Test usage of this function:
# test = getBudgetAccount(year=2009, income=50000, agency=3815)
# Generate the API query:
query = paste(prefix,"getReceiptAggregate?",sep="")
params <- list("year"=year,"exacttax"=exacttax,"category"=categ,
"subcategory"=subcateg,"agency"=agency,"bureau"=bureau,"account"=account,
"adjustInflationYear"=adjInflYear,"type"=type,"sortby"=sortby,
"sortdir"=sortdir,"filing"=filing,"selfEmployed"=selfEmployed,
"showExtra"=showExtra,"showChange"=showChange,"group"=group)
arguments = pasteArgs(params)
query = paste(query, arguments, sep="")
# Get the result of the query
xmlResult <- xmlRoot(xmlTreeParse(query))
# Get attribute values from the api call
result <- xmlToDataframe(xmlResult)
return(result)
}
getPopulation <- function(startYear, endYear=startYear) {
query = paste(prefix,"getPopulation?",sep="")
years = startYear:endYear
result <- getYearsFrame(query, years)
return(result)
}
getGDP <- function(startYear, endYear=startYear) {
query = paste(prefix,"getGDP?",sep="")
years = startYear:endYear
result <- getYearsFrame(query, years)
return(result)
}
getDebt <- function(startYear, endYear=startYear) {
query = paste(prefix,"getDebt?",sep="")
years = startYear:endYear
result <- getYearsFrame(query,years)
return(result)
}
getInflation <- function(startYear, endYear=startYear) {
query = paste(prefix,"getInflation?",sep="")
years = startYear:endYear
result <- getYearsFrame(query,years)
return(result)
}
getYearsFrame <- function(query, years) {
result = data.frame()
for(i in years) {
thisQuery = paste(query, "year=", i, sep="")
xmlResult <- xmlRoot(xmlTreeParse(thisQuery))
newResult <- yearToFrame(xmlResult, i)
result <- rbind(result,newResult)
}
return(result)
}
yearToFrame <- function(xmlDoc, year) {
result <- xmlSApply(xmlDoc, function(x) xmlAttrs(x))
result <- t(result)
row.names(result) <- year
result <- as.data.frame(result)
return(result)
}
# TODO There's a bug in here:
getTaxRates <- function(startYear, endYear=startYear, type="") {
query = paste(prefix,"getTaxRates?",sep="")
if(nchar(type) > 0) {
query = paste(query,"type=",type,"&",sep="")
}
years = startYear:endYear
result <- data.frame()
j = 1 # indices of the dataframe
for(i in years) {
thisQuery = paste(query, "year=", i, sep="")
xmlResult <- xmlRoot(xmlTreeParse(thisQuery))
newResult <- xmlSApply(xmlResult, function(x) xmlAttrs(x))
newResult <- t(newResult)
row.names(newResult) <- j:(j+nrow(newResult) - 1)
result <- rbind(result,newResult)
j = j + nrow(result)
}
return(result)
}
pasteArgs <- function(params) {
arguments = ""
for(name in names(params)) {
if(nchar(params[[name]]) > 0) {
# If the argument isn't empty, add it to the arg list
arguments = paste(arguments, name, "=", params[[name]], "&", sep="")
}
}
# Remove the trailing '&'
arguments = substr(arguments, 1, nchar(arguments) - 1)
return(arguments)
}
xmlToDataframe <- function(xmlDoc) {
result <- xmlSApply(xmlDoc, function(x) xmlAttrs(x))
result <- t(result)
row.names(result) <- 1:nrow(result)
result <- as.data.frame(result)
return(result)
}