[API] spiR

API & Databases R Courses

Access open data on the Social Progress Index and more through the spiR API.

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
01-31-2020

Database description

“The Social Progress Index is a new way to define the success of our societies. It is a comprehensive measure of real quality of life, independent of economic indicators. The Social Progress Index is designed to complement, rather than replace, economic measures such as GDP.” (The Social Progress Imperative, 2018)

Social Progress : https://www.socialprogress.org/

Functions

This package lets you recreate impactful dashboards and visualizations as the ones found on the Social Progress Imperative. This API provides one main function, spi_data(), which lets you extract the data in a convenient format and two other functions, spi_country() and spi_indicator(), that can assist you finding the appropriate arguments for the API.

Some examples are provided below.

spi_country()

This function allows you to find and search the right country code associated with the Social Progress Index’s Data. If no argument is filed, all indicators will be displayed.

#Loading the sipR package
library(spiR)

#Get the ISO code for a specific country
myCountry <- spi_country("Canada")
myCountry
# A tibble: 1 x 2
  countryName code 
  <chr>       <chr>
1 Canada      CAN  

spi_indicator()

This function allows you to find and search the right indicator code from the Social Progress Index you want to use. If no argument is filed, all indicators will be displayed.

#Search for an indicator
myIndicator <- spi_indicator("mortality")
myIndicator
# A tibble: 2 x 7
  indicator_name indicator_code indicator_defin… indicator_source
  <chr>          <chr>          <chr>            <chr>           
1 Maternal mort… NBM_2          Maternal deaths… Institute for H…
2 Child mortali… NBM_3          Probability of … UN Inter-agency…
# … with 3 more variables: indicator_source_link <chr>,
#   indicator_dimension <chr>, indicator_component <chr>

spi_data()

First, the function spi_data() takes as an input the countries we’re interested in. We specify this argument with the countries ISO code, as such: c(“USA”, “FRA”, “BRA”, “CHN”, “ZAF”, “CAN”). The second argument is dedicated for the years for which we want data. Finally, we need to specify the indicator from Social Progress we would like to extract.

For example, let’s take a look at the SPI indicator (Social Progress Index) for the countries listed above.

#Extracting the data
myData <- spi_data(country = c("USA", "FRA", "BRA", "CHN", "ZAF", "CAN"), 
                       year = c("2014","2015","2016", "2017", "2018", "2019"), 
                       indicators = "SPI")
myData
     countryName var_code var_year var_indicator value
1         Brazil      BRA     2014           SPI 73.59
2         Canada      CAN     2014           SPI 86.97
3          China      CHN     2014           SPI 61.58
4         France      FRA     2014           SPI  87.1
5   South Africa      ZAF     2014           SPI 64.65
6  United States      USA     2014           SPI 84.74
7         Brazil      BRA     2015           SPI 73.45
8         Canada      CAN     2015           SPI 87.17
9          China      CHN     2015           SPI 62.38
10        France      FRA     2015           SPI 87.19
11  South Africa      ZAF     2015           SPI 65.38
12 United States      USA     2015           SPI 84.71
13        Brazil      BRA     2016           SPI 74.12
14        Canada      CAN     2016           SPI 87.25
15         China      CHN     2016           SPI 62.89
16        France      FRA     2016           SPI 87.48
17  South Africa      ZAF     2016           SPI 66.19
18 United States      USA     2016           SPI 85.09
19        Brazil      BRA     2017           SPI  72.8
20        Canada      CAN     2017           SPI 87.79
21         China      CHN     2017           SPI 63.73
22        France      FRA     2017           SPI  87.6
23  South Africa      ZAF     2017           SPI 66.74
24 United States      USA     2017           SPI 84.18
25        Brazil      BRA     2018           SPI 72.66
26        Canada      CAN     2018           SPI  88.6
27         China      CHN     2018           SPI 64.16
28        France      FRA     2018           SPI 87.69
29  South Africa      ZAF     2018           SPI 66.56
30 United States      USA     2018           SPI 83.85
31        Brazil      BRA     2019           SPI 72.87
32        Canada      CAN     2019           SPI 88.81
33         China      CHN     2019           SPI 64.54
34        France      FRA     2019           SPI 87.79
35  South Africa      ZAF     2019           SPI 67.44
36 United States      USA     2019           SPI 83.62

We can now compare these countries performances based on the SPI indicator.

tl;dr

#Loading the spir package
library(spiR)

#Get the ISO code for a specific country
mycountry <- spi_country("Canada")
mycountry

#Search for an indicator
myIndicator <- spi_indicator("mortality")
myIndicator

#Extracting the data
myData <- spi_data(country = c("USA", "FRA", "BRA", "CHN", "ZAF", "CAN"),
                   year = c("2014","2015","2016", "2017", "2018", "2019"),
                   indicators = "SPI")

Code learned this week

Command Detail
spi_data() Find data related to the indicators for each country
spi_country() Search for a country’s ISO code
spi_indicator() Search for a Social Progress indicator

References

This tutorial uses the spiR package documentation


Citation

For attribution, please cite this work as

Warin (2020, Jan. 31). Thierry Warin, PhD: [API] spiR. Retrieved from https://warin.ca/posts/api-spir/

BibTeX citation

@misc{warin2020[api],
  author = {Warin, Thierry},
  title = {Thierry Warin, PhD: [API] spiR},
  url = {https://warin.ca/posts/api-spir/},
  year = {2020}
}