3. Make a more complex call

Make a more complex call

Some calls require additional API variables like the state or county of interest. For these calls, we can easily add variables to the base call by passing them in as a second argument to perform.call().

Adding a single variable

For example, suppose we want to find out what counties and respective parameter codes exist for North Carolina. We can again use the services object, and check its List service as in Making your first call.

It looks like the Counties by state filter would be appropriate since we’re trying to determine the county parameter codes for North Carolina.

services$List$Filters$`Counties by State`
## $Endpoint
## [1] "list/countiesByState"
## 
## $RequiredVariables
## [1] "email, key, state"
## 
## $OptionalVariables
## [1] ""
## 
## $Example
## [1] "Example; returns all counties in North Carolina:https://aqs.epa.gov/data/api/list/countiesByState?email=test@aqs.api&key=test&state=37"

Notice we have a required variable state. Since we’re looking for counties in North Carolina, we can use the parameter code for North Carolina as the entry for state. We were able to determine all parameter codes for states in Making your first call so we look at our output there and find that the parameter code for North Carolina is 37.

Now, we can perform the call by putting together the endpoint for listing counties by state and the particular state we’re interested.

endpoint <- services$List$Filters$`Counties by State`$Endpoint
state.code <- '37'
result <- perform.call(endpoint = endpoint, variables = state.code, name = "state")

Note that we specified the additional variables we’d be adding with variables = state.code, and we inserted the API variable name as name = state in the function. An alternative is to simply declare the parameter code with the API variable name as follows.

endpoint <- services$List$Filters$`Counties by State`$Endpoint
state <- '37'
result <- perform.call(endpoint = endpoint, variables = state)

Adding multiple variables

In the case of multiple variables, we can make a list of variables and pass the list into perform.call() as the second argument.

Suppose, for instance, we’re interested in finding daily ozone data for North Carolina, starting January 1, 2020 and ending on January 2, 2020, and we have determined the correct parameter codes for each API variable (please see Navigating API services or Figuring out API variables to understand how we could go about determining proper parameter codes for each of these variables).

Our first option is to make a list of the parameter codes and a list of API variable names that correspond to the parameter codes.

endpoint <- 'dailyData/byState'
variable.list <- list('37','20200101','20200102','44201')
result <- perform.call(endpoint = endpoint, 
                       variables = variable.list, 
                       name = list("state", "bdate", "edate", "param"))

Our second option is to create a list that has the appropriate API variable names declared for parameter codes.

endpoint <- 'dailyData/byState'
variable.list <- list("state" = '37', 
                      "bdate" = '20200101', 
                      "edate" = '20200102', 
                      "param" = '44201')
result <- perform.call(endpoint = endpoint, variables = variable.list)