The retrieve data functions are based on the OxCGRT’s JSON API described here. Two API endpoints are provided: 1) endpoint for JSON providing data for stringency index by country over time; and, 2) endpoint for JSON providing data on policy actions and stringency index for a specific country on a specific day.
The first API endpoint provides JSON for all countries included in the OxCGRT over a specified period of time:
https://covidtrackerapi.bsg.ox.ac.uk/api/v2/stringency/date-range/{start-date}/{end-date}
where start-date
and end-date
are the starting date and ending date (both in YYYY-MM-DD
format) respectively from which to retrieve data.
The oxcgrt
package provides a function named get_json_time
to interface with the API and retrieve the specified JSON and a function named get_data_time
to extract the data from the specified JSON into an R tibble
object. These two functions have been designed such that they can be piped from one to the other. Hence to retrieve stringency index data for all countries from 1 June 2020 to current date, the following code can be used:
This produces the following output:
#> # A tibble: 31,480 x 9
#> date_value country_code country_name confirmed deaths stringency_actu…
#> <date> <chr> <chr> <int> <int> <dbl>
#> 1 2020-06-01 ABW Aruba 101 3 50
#> 2 2020-06-01 AFG Afghanistan 15205 257 84.3
#> 3 2020-06-01 AGO Angola 86 4 77.8
#> 4 2020-06-01 ALB Albania 1137 33 71.3
#> 5 2020-06-01 AND Andorra 764 51 50
#> 6 2020-06-01 ARE United Arab… 34557 264 72.2
#> 7 2020-06-01 ARG Argentina 16838 539 90.7
#> 8 2020-06-01 AUS Australia 7195 102 62.0
#> 9 2020-06-01 AUT Austria 16642 668 53.7
#> 10 2020-06-01 AZE Azerbaijan 5494 63 77.8
#> # … with 31,470 more rows, and 3 more variables: stringency <dbl>,
#> # stringency_legacy <dbl>, stringency_legacy_disp <dbl>
Important to note that in get_json_time
, only the starting date (using the from
argument) is specified to the desired 1 June 2020 in YYYY-MM-DD
format. This is because by default the to
argument (for the ending date) is set to the current date using a call to the Sys.Date()
function. By default, the from
argument is set to 2 January 2020 (2020-01-02) which is the earliest available data point for the stringency index. Therefore, to retrieve data on stringency index for all countries for all available time points up to current, the following commands can be issued:
which produces the following output:
#> # A tibble: 58,315 x 9
#> date_value country_code country_name stringency_actu… stringency
#> <date> <chr> <chr> <dbl> <dbl>
#> 1 2020-01-02 PSE Palestinian… 0 0
#> 2 2020-01-02 QAT Qatar 0 0
#> 3 2020-01-02 ROU Romania 0 0
#> 4 2020-01-02 RUS Russia 0 0
#> 5 2020-01-02 RWA Rwanda 0 0
#> 6 2020-01-02 SAU Saudi Arabia 0 0
#> 7 2020-01-02 SDN Sudan 0 0
#> 8 2020-01-02 SEN Senegal 0 0
#> 9 2020-01-02 SGP Singapore 13.9 13.9
#> 10 2020-01-02 SLB Solomon Isl… 0 0
#> # … with 58,305 more rows, and 4 more variables: stringency_legacy <dbl>,
#> # stringency_legacy_disp <dbl>, confirmed <int>, deaths <int>
The second API endpoint provides JSON for a specific country included in the OxCGRT for a specified day:
https://covidtrackerapi.bsg.ox.ac.uk/api/v2/stringency/actions/{country-code}/{date}
where country-code
is the ISO 3166-1 alpha-3 country code for the required country to get data for and date
is the date (in YYYY-MM-DD
format) on which to retrieve data.
The oxcgrt
package provides a function named get_json_actions
to interface with the API and retrieve the specified JSON and a function named get_data
to extract the data from the specified JSON into a named list
R object. These two functions have been designed such that they can be piped from one to the other. Hence to retrieve policy actions and stringency index data for Afghanistan for 1 June 2020, the following code can be used:
which produces the following output:
#> $policyActions
#> # A tibble: 18 x 9
#> policy_type_code policy_type_dis… policyvalue policyvalue_act… flagged
#> <chr> <chr> <int> <int> <lgl>
#> 1 C1 School closing 3 3 TRUE
#> 2 C2 Workplace closi… 3 3 FALSE
#> 3 C3 Cancel public e… 2 2 TRUE
#> 4 C4 Restrictions on… 4 4 TRUE
#> 5 C5 Close public tr… 2 2 FALSE
#> 6 C6 Stay at home re… 2 2 FALSE
#> 7 C7 Restrictions on… 2 2 FALSE
#> 8 C8 International t… 3 3 NA
#> 9 E1 Income support 0 0 NA
#> 10 E2 Debt/contract r… 0 0 NA
#> 11 E3 Fiscal measures 0 0 NA
#> 12 E4 International s… 0 0 NA
#> 13 H1 Public informat… 2 2 TRUE
#> 14 H2 Testing policy 1 1 NA
#> 15 H3 Contact tracing 1 1 NA
#> 16 H4 Emergency inves… 0 0 NA
#> 17 H5 Investment in v… 0 0 NA
#> 18 H6 Facial Coverings 1 1 TRUE
#> # … with 4 more variables: is_general <lgl>, notes <chr>,
#> # flag_value_display_field <chr>, policy_value_display_field <chr>
#>
#> $stringencyData
#> # A tibble: 1 x 6
#> date_value country_code confirmed deaths stringency_actual stringency
#> <chr> <chr> <int> <int> <dbl> <dbl>
#> 1 2020-06-01 AFG 15205 257 84.3 84.3
Important to note that the output is a named list
object containing a tibble
of policy actions data and a tibble
of stringency index data for the specified country and the specified date.
It is also possible to retrieve just policy actions data for a specific country or for multiple countries on a specific day or multiple days. To retrieve policy actions data for Afghanistan for 1 June 2020, the following code can be used:
This results in:
#> # A tibble: 18 x 12
#> date_value country_code country_name policy_type_code policy_type_dis…
#> <date> <chr> <chr> <chr> <chr>
#> 1 2020-06-01 AFG Afghanistan C1 School closing
#> 2 2020-06-01 AFG Afghanistan C2 Workplace closi…
#> 3 2020-06-01 AFG Afghanistan C3 Cancel public e…
#> 4 2020-06-01 AFG Afghanistan C4 Restrictions on…
#> 5 2020-06-01 AFG Afghanistan C5 Close public tr…
#> 6 2020-06-01 AFG Afghanistan C6 Stay at home re…
#> 7 2020-06-01 AFG Afghanistan C7 Restrictions on…
#> 8 2020-06-01 AFG Afghanistan C8 International t…
#> 9 2020-06-01 AFG Afghanistan E1 Income support
#> 10 2020-06-01 AFG Afghanistan E2 Debt/contract r…
#> 11 2020-06-01 AFG Afghanistan E3 Fiscal measures
#> 12 2020-06-01 AFG Afghanistan E4 International s…
#> 13 2020-06-01 AFG Afghanistan H1 Public informat…
#> 14 2020-06-01 AFG Afghanistan H2 Testing policy
#> 15 2020-06-01 AFG Afghanistan H3 Contact tracing
#> 16 2020-06-01 AFG Afghanistan H4 Emergency inves…
#> 17 2020-06-01 AFG Afghanistan H5 Investment in v…
#> 18 2020-06-01 AFG Afghanistan H6 Facial Coverings
#> # … with 7 more variables: policyvalue <int>, policyvalue_actual <int>,
#> # flagged <lgl>, is_general <lgl>, notes <chr>,
#> # flag_value_display_field <chr>, policy_value_display_field <chr>
Important to note here that the output is a tibble of just the policy actions and three additional columns have been added to the dataset - date_value
, country_code
, and country_name
- to identify the data as coming from a specific date and for a specific country.
To retrieve policy actions data for multiple countries on multiple days, the get_data_actions
functions can be used as shown below:
get_json_actions(ccode = c("AFG", "Philippines"),
from = "2020-10-25",
to = "2020-10-31") %>%
get_data_actions()
This results in:
#> # A tibble: 252 x 12
#> date_value country_code country_name policy_type_code policy_type_dis…
#> <date> <chr> <chr> <chr> <chr>
#> 1 2020-10-25 AFG Afghanistan C1 School closing
#> 2 2020-10-25 AFG Afghanistan C2 Workplace closi…
#> 3 2020-10-25 AFG Afghanistan C3 Cancel public e…
#> 4 2020-10-25 AFG Afghanistan C4 Restrictions on…
#> 5 2020-10-25 AFG Afghanistan C5 Close public tr…
#> 6 2020-10-25 AFG Afghanistan C6 Stay at home re…
#> 7 2020-10-25 AFG Afghanistan C7 Restrictions on…
#> 8 2020-10-25 AFG Afghanistan C8 International t…
#> 9 2020-10-25 AFG Afghanistan E1 Income support
#> 10 2020-10-25 AFG Afghanistan E2 Debt/contract r…
#> # … with 242 more rows, and 7 more variables: policyvalue <int>,
#> # policyvalue_actual <int>, flagged <lgl>, is_general <lgl>, notes <chr>,
#> # flag_value_display_field <chr>, policy_value_display_field <chr>
Important to note here that the output is a tibble of just the policy actions and three additional columns have been added to the dataset - date_value
, country_code
, and country_name
- to identify the data as coming from a specific date and for a specific country.