Skip to contents

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.

Stringency index by country over time

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:

get_json_time(from = "2020-06-01") |> get_data_time()

This produces the following output:

#> # A tibble: 173,696 × 9
#>    date_value country_code country_name       confirmed deaths stringency_actual
#>    <date>     <chr>        <chr>                  <int>  <int>             <dbl>
#>  1 2020-06-01 ABW          Aruba                    101      3              57.4
#>  2 2020-06-01 AFG          Afghanistan            15836    269              84.3
#>  3 2020-06-01 AGO          Angola                    86      4              77.8
#>  4 2020-06-01 ALB          Albania                 1143     33              71.3
#>  5 2020-06-01 AND          Andorra                  765     51              42.6
#>  6 2020-06-01 ARE          United Arab Emira…     35192    266              72.2
#>  7 2020-06-01 ARG          Argentina              17415    556              90.7
#>  8 2020-06-01 AUS          Australia               7221    102              62.0
#>  9 2020-06-01 AUT          Austria                16642    741              53.7
#> 10 2020-06-01 AZE          Azerbaijan              5662     68              75.9
#> # ℹ 173,686 more rows
#> # ℹ 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: 201,480 × 9
#>    date_value country_code country_name       confirmed deaths stringency_actual
#>    <date>     <chr>        <chr>                  <int>  <int>             <dbl>
#>  1 2020-01-02 ABW          Aruba                      0      0                 0
#>  2 2020-01-02 AFG          Afghanistan                0      0                 0
#>  3 2020-01-02 AGO          Angola                     0      0                 0
#>  4 2020-01-02 ALB          Albania                    0      0                 0
#>  5 2020-01-02 AND          Andorra                    0      0                 0
#>  6 2020-01-02 ARE          United Arab Emira…         0      0                 0
#>  7 2020-01-02 ARG          Argentina                  0      0                 0
#>  8 2020-01-02 AUS          Australia                  0      0                 0
#>  9 2020-01-02 AUT          Austria                    0      0                 0
#> 10 2020-01-02 AZE          Azerbaijan                 0      0                 0
#> # ℹ 201,470 more rows
#> # ℹ 3 more variables: stringency <dbl>, stringency_legacy <dbl>,
#> #   stringency_legacy_disp <dbl>

Policy actions and stringency index for specific country on a specific day

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:

get_json_actions(ccode = "AFG", 
                 from = NULL, 
                 to = "2020-06-01") |> 
  get_data()

which produces the following output:

#> $policyActions
#> # A tibble: 23 × 9
#>    policy_type_code policy_type_display   policyvalue policyvalue_actual flagged
#>    <chr>            <chr>                       <int>              <int> <lgl>  
#>  1 C1               School closing                  3                  3 TRUE   
#>  2 C2               Workplace closing               3                  3 FALSE  
#>  3 C3               Cancel public events            2                  2 TRUE   
#>  4 C4               Restrictions on gath…           4                  4 TRUE   
#>  5 C5               Close public transpo…           2                  2 FALSE  
#>  6 C6               Stay at home require…           2                  2 FALSE  
#>  7 C7               Restrictions on inte…           2                  2 FALSE  
#>  8 C8               International travel…           3                  3 NA     
#>  9 E1               Income support                  0                  0 NA     
#> 10 E2               Debt/contract relief            0                  0 NA     
#> # ℹ 13 more rows
#> # ℹ 4 more variables: is_general <lgl>, notes <chr>,
#> #   flag_value_display_field <chr>, policy_value_display_field <chr>
#> 
#> $stringencyData
#> # A tibble: 1 × 6
#>   date_value country_code confirmed deaths stringency_actual stringency
#>   <chr>      <chr>            <int>  <int>             <dbl>      <dbl>
#> 1 2020-06-01 AFG              15836    269              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.

Policy actions for specific country or countries on a specific day or days

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:

get_json_actions(ccode = "AFG", 
                 from = NULL, 
                 to = "2020-06-01") |> 
  get_data_action()

This results in:

#> # A tibble: 23 × 12
#>    date_value country_code country_name policy_type_code policy_type_display    
#>    <date>     <chr>        <chr>        <chr>            <chr>                  
#>  1 2020-06-01 AFG          Afghanistan  C1               School closing         
#>  2 2020-06-01 AFG          Afghanistan  C2               Workplace closing      
#>  3 2020-06-01 AFG          Afghanistan  C3               Cancel public events   
#>  4 2020-06-01 AFG          Afghanistan  C4               Restrictions on gather…
#>  5 2020-06-01 AFG          Afghanistan  C5               Close public transport 
#>  6 2020-06-01 AFG          Afghanistan  C6               Stay at home requireme…
#>  7 2020-06-01 AFG          Afghanistan  C7               Restrictions on intern…
#>  8 2020-06-01 AFG          Afghanistan  C8               International travel c…
#>  9 2020-06-01 AFG          Afghanistan  E1               Income support         
#> 10 2020-06-01 AFG          Afghanistan  E2               Debt/contract relief   
#> # ℹ 13 more rows
#> # ℹ 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: 322 × 12
#>    date_value country_code country_name policy_type_code policy_type_display    
#>    <date>     <chr>        <chr>        <chr>            <chr>                  
#>  1 2020-10-25 AFG          Afghanistan  C1               School closing         
#>  2 2020-10-25 AFG          Afghanistan  C2               Workplace closing      
#>  3 2020-10-25 AFG          Afghanistan  C3               Cancel public events   
#>  4 2020-10-25 AFG          Afghanistan  C4               Restrictions on gather…
#>  5 2020-10-25 AFG          Afghanistan  C5               Close public transport 
#>  6 2020-10-25 AFG          Afghanistan  C6               Stay at home requireme…
#>  7 2020-10-25 AFG          Afghanistan  C7               Restrictions on intern…
#>  8 2020-10-25 AFG          Afghanistan  C8               International travel c…
#>  9 2020-10-25 AFG          Afghanistan  E1               Income support         
#> 10 2020-10-25 AFG          Afghanistan  E2               Debt/contract relief   
#> # ℹ 312 more rows
#> # ℹ 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.