
FAOSTAT
over the last 25 years (Strobel 2018)
FAOSTAT
3.0A revitalisation of a wrapper for the FAOstat API
The motivation for this project came from a Data Mining project from UniLaSalle. It was suggested that we use FAO1 data from their statistical platform FAOstat2. As R was the language of choice, the obvious port of call was the FAOSTAT
package3 (Kao, Gheri, and Gesmann 2022), developed by employees at FAO.
However, the FAOSTAT
package did not work. It could not download data from the API4 and could only download bulk data with the entirety of a dataset in one go. For the particular dataset we were interested in, we found that there was a discrepancy between the data in the bulk download and the data on the web platform.5
Eventually it became necessary to use the same API that the FAOstat website uses to pull data. This method worked and it became clear that it could be used to revitalise the FAOSTAT
package and part of an effort to restore it to full functionality.
A first update, FAOSTAT
2.3.0 was released by the author in May 2023. FAOSTAT
3.0 is a continuation of that effort and realises many of the goals that it set out.
An API6 is a method for a host application to allow a client to send it commands in a way the client understands. The API is a layer that converts these client commands to ones that the host understands. To use a metaphor, you could in theory drive a car by manually turning the axle to got left and start it by hitting a spark plug yourself. However, the key (or start button, if you have a newer model) and steering wheel are a much more natural and intuitive way to direct a car for a human being. However, from the car’s perspective the axle gets turned and the spark plug is struck. The steering wheel and key essentially work as an API between the server (the car) and the client (the person).
In modern parlance, when we refer to an API, we usually mean a REST7 API. This is a type of API run over the web via HTTP8, the same protocol that serves web pages. These APIs are a method of exchanging data and use the following major verbs9 from HTTP:
GET - Request data from the server
GET https://example.com/johnsmith/info
{username: johnsmith, full_name = "John Smith"}
PUT - Send data to the server
PUT https://example.com/johnsmith/info
{full_name: "John Allen Smith"}
POST - Send data to the server and receive data
POST https://example.com/johnsmith/send_email
{subject: "Business opportunity", body = "Dear {fullname}, please reply"}
{result: "success", send_time: "2020-09-23 17:34:15"}
DELETE - Delete data from the server
DELETE https://example.com/johnsmith
The key innovation that REST introduces is statelessness. The server does not have to keep track of any previous requests. Each request is atomic and is considered in isolation. This distinguishes it from other API patterns such as SOAP10 (Halili and Ramadani (2018)).
The FAO11 is a specialised agency of the UN12. This means that it is not a direct organ of the UN, but reports to the UN Economic and Social Council13. Its goal is to:
…achieve food security for all and make sure that people have regular access to enough high-quality food to lead active, healthy lives. (FAO (2024a))
As part of its mission, it collects food statistics from all member countries of the UN. It published its first account of the world food supply in 1946 with the publication of the World Food Survey (FAO (1946)). This mission of collecting food statistics has continued today with the Statistics Division in FAO. This division has three main goals (FAO (2024b)):
Produce updated statistics on food and nutrition security, sustainable agriculture and rural development at national and international levels;
Develop and promote international food and agricultural statistical standards, as well as methods and tools for collecting, analyzing and disseminating data;
Work directly with countries to develop national statistical strategies, strengthen institutional and technical capacities and improve statistical systems.
In order to disseminate these produced statistics, one requires a tool. This tool is FAOstat.
FAOstat14 is FAO’s web-based statistical platform for the free dissemination of food and agriculture statistics. This data is obtained from questionnaires that FAO distributes throughout the world every year (FAO 2019). Some of its data also comes from imputations and models where data is not available, but official country data takes precedence.
FAOstat was first released in 1997. As the internet was not as widespread as today, it was largely distributed as a database on CD-ROM, continuing until at 2005 at the latest (United Nations (2005)). It is currently a 1.5 GB download (FAO (2023)). It now also includes methods of displaying data and interacting with them using javascript libraries Leaflet15 and Highchart16.
FAOSTAT
over the last 25 years (Strobel 2018)
The FAOstat service is a public-facing aspect of FAO, with an overall trend of increasing citations in academic papers year on year with 21400 citations by 2021 (Figure 1).
This platform uses a REST API internally to communicate with its database as well as providing a set of zip files with the entirety of certain datasets in order to reduce the load on the database. This REST API allows the website to generate CSVs as well as to allow exploration of the data via interactive graphs (Figure 2).
FAOSTAT
packageThe FAOSTAT
package is an API wrapper to pull data from FAOstat into a R session. It can also perform small necessary tasks such as country code conversion and coalescing data from different country groups.17
The FAOSTAT
package was originally developed in 2013 as a tool to source data for the SYB18 project. The yearbooks are yearly summaries of the worldwide state of agriculture for that year. At the time, they were manually typeset and compiled. The new SYB project was to use a combination of LaTeX
, knitr
and R
to automatically pull data from FAOstat and other data sources such as the World Bank. This data would be then be transformed and processed to create graphs and tables before finally formatting and typesetting to create a finished product which could then be printed.19. Given that this use case no longer exists, the primary use of this package is for researchers and other R users to read data from FAOstat in a clean way that makes it easier to move to analysis afterwards.
It is a reasonably popular package; in the 86th percentile of all packages on CRAN on 2023-04-01 by downloads. In total, the package has been downloaded over 50 000 times with a peak 121 daily downloads on 2019-05-15. (Li 2023)
%%{init: {'timeline': {'disableMulticolor': true} } }%% timeline title History of the FAOSTAT package 2013 : Michael Kao creates the FAOSTAT package on GitHub 2014 : Filippo Gheri takes maintainership FAOSTAT 2017 : FAOSTAT3 API is deprecated 2020 : Paul Rougieux takes maintainership of FAOSTAT : FAOSTAT moves to GitLab 2023 : Sebastian Campbell begins work on FAOSTAT : FAOSTAT 0.2.3 released 2024 : FAOSTAT 3.0.0 released
FAOSTAT
package from its creation to today
The package was maintained by Michael Kao, the author, from 2013 to 2014. In 2014, it was maintained by Filippo Gheri before passing to Paul Rougieux (the current maintainer) in 2020 (Figure 3).
While it was originally hosted on Github under Michael Kao’s personal account, It is currently hosted on GitLab under Paul Rougieux’s personal account.
FAOSTAT
git repository by number of commits and by number of lines contributed that remain in the current master branch. Snapshot taken on 2024-02-26
While there have been other contributors, they have only provided occasional commits and very little of that code remains in the codebase (Figure 4). The major four maintainers (Kao, Gheri, Rougieux and Campbell) are by far the main contributors.
FAOSTAT
git repository over time by author. Snapshot taken on 2024-02-26
The package’s development can be split into two major periods. Between 2013 and 2017 (Figure 5), the package is focused on the Statistical Yearbooks and the FAOSTAT3 API. However, as the package is no longer used for the yearbook and the API is discontinued, the focus is changed to the bulk download functionality to download whole datasets at once.
flowchart TD Group --> Domain Dimension --> Codes Dimension --> Subdimensions Subdimensions --> Codes Domain --> Dimension Domain --> Data Domain --> BD[Bulk downloads] Domain --> Metadata Metadata --> Document
The API is has two main components: dimensions and data (Figure 6). Domains are the objects that hold data itself and these are clustered into groups. Groups link thematically connected domains. These groups correspond to the various internal teams within FAO that are responsible for disseminating data.20 Dimensions and subdimensions for the definitions for the shape of the data. These are defined separately and can be applied to multiple domains. In addition there are utility endpoints:
/ping
- simple check to see if the server is up/datasize
- Appended to the end of queries, returns the size of the dataThe FAOSTAT
package has only a shadow of its former functionality. While it has retained the ability to download and process zip files (bulk downloads) and country code processing functions,21 its capacities are limited by the following issues:
A number of functions are simply designed to pull in data from other sources such as the World Bank and to process that data into a format easily consumed by the Statistical Yearbook. As the yearbook no longer uses the FAOSTAT
package, these functions have no further purpose, serving only to clog up the package and its help files.
Many uses of FAOSTAT require data outside of the data that comes directly from FAOstat The major use case is for code conversions. There are two main code types that require conversion:
The conversions are not dynamically taken from the API but rather stored in a fixed file. This makes them vulnerable to code or name changes in the future such as the name change of Swaziland to Eswatini in 2018 (United Nations 2018).
The FAOSTAT
package is currently configured to access a now-defunct API23 (FAOSTAT3). As a result, it has no methods of retrieving data from the FAOstat platform with the sole exception of the bulk zip downloads which have been since adapted to use the current platform.
When making a query using the API, the request must contain exactly four dimensions (area, item, element and year). This means that it cannot be used for domains that do not meet these strict design requirements. For example, the GFDI24 domain has 6 dimensions (area, category, industry, factor, element, year)
The FAOSTAT
package is currently maintained by Paul Rougieux who has done an excellent job of keeping the package afloat. However, it is a small project done in his free time, so he has not been able to make the time to do a full overhaul. In addition, he is not an employee of FAO, but rather of the European Commission. As a face that FAO shows to the world, it seems reasonable that it be placed under its guidance.
As the package is old, it also has a number of dependencies that are unused or will be, after redundant functions are removed. Other dependencies are simply no longer developed or have been superseded by newer packages. This means that installing the packages automatically installs a number of other packages that are of no use, increase install time, space used and being generally inelegant.
There are four main goals of this project:
The first priority to restore the FAOSTAT
package is to repair the most basic and most used functions. These are the functions that work directly with the API and that provide data to the user.25 After this point, functions should not only be repaired but also renamed to have a consistent naming system. The existing functions do have a naming convention (Camel case, consistent use of verbs) but it doesn’t reflect the terminology used in the API and there’s no internal hierarchy to naming.
As described in the Current state section, many functions no longer work and need to be removed. This involves a thorough listing of them and their functionality and assessment of their usefulness. A full analysis was completed for the release of FAOSTAT
2.3.0 and is available on GitLab.26
Inspecting the API to find all relevant endpoints was completed with FAOSTAT
version 2.3.0 released in March 2023.27 This requires the additional step of inspecting the API manually as it does not have complete documentation.
Uploading a package on CRAN28 requires the permission of the maintainer. At the upload step, an email is sent to the maintainer of the last recorded package version to confirm that the upload was authorised. Only then can the new version proceed to the registration step. FAOSTAT
2.3.0 was released via this process, but maintainership needs to be transferred to do it with full autonomy.
On 2023-03-30, the FAOSTAT
package version 2.3.0 was published to CRAN.29 The process required the resolution of the issues detailed in the Project Goals section.30
In order to solve these issues, a full examination of the package context, the API and a redesign of existing functions needed to be made. These efforts permitted the eventual release of FAOSTAT
3.0.0.
In the above Identified Problems section, we described the following issues:
Of all the functions in the FAOSTAT
package, we identified eleven functions that we inextricably linked to the Statistical Yearbook. They were either linked to text formatting (e.g. printLab
) or obtaining auxiliary data from third party sources like the World Bank (e.g. getWDI
). These were all identified31 and removed32.
For the most part, they could be easily identified by the use of “SYB” in the function name or reference to non-FAO sources like the World Bank. In more advanced cases, we would simply examine the functions for reference to the FAOstat API and discard all functions that did not and were not called by any functions that did use it.
The following data is stored in local files:
FAOcountryProfile
)Q
is the code in the API for the Production domain) (FAOmetaTable
)FAOregionProfile
)These data (with the exception of ISO 2-character codes) are all obtainable from the API itself (the various /dimensions
endpoints), so it does not make any sense to retain these. Country code conversion beyond converting between formats used by the API is not in the scope of this package. The recommended way to convert country codes for the purpose of analysis is to use the excellent countrycode
package (Arel-Bundock, Enevoldsen, and Yetman (2018)). Not only can this package convert between codes, it is also capable of using regular expressions on country names, converting full text country names into the appropriate codes.
The FAOstat3 API ceased functioning in 2017 and was replaced by the API that is currently being used to feed the the FAOstat data explorer.
The new API however, wasn’t designed for other applications to consume it. This manifested in two ways:
The API is not fully documented as the only official consumer of the API is the FAOstat web application itself. Third party development isn’t in scope for the current project cycle. The main documentation is a JSON:API specification,34 which describes the structure perfectly, but gives no context or recommendations for use, nor does it describe output. This is intentional as the servers which provide the API are not equipped to handle the load that a publicly available API would inflict. In one instance, the response would be cut off half-way through with no warning or error as the server was overloaded.35
It was thus necessary to use this document to manually examine all the endpoints and characterise all the the data output from them.36
As a result, we have created the most accessible documentation for the FAOstat API. We have also negotiated that the development of this API package continue. The main arguments are that R is mainly used for scripts by individual users rather than for automated routines in a production environment (Morandat et al. (2012)), so it’s unlikely that there will be any issues. Furthermore, a server upgrade occurred in February 2024, increasing their capacity to handle additional requests.
The limited extensibility of FAOSTAT
manifests in two ways:
This inflexibility causes problems for unusual data structures or changes to codes in FAOstat. Essentially, any changes that are made could cause complete breakage for certain requests, for example the update to the CPC codes in 2015 (United Nations Statistical Division (2015)). Fixing this issue requires us to depend more heavily on the API.
We solved this issue by using the same endpoints that the API uses to convert codes when providing them to the user for data view or for downloads. The API has /definitions
endpoints that contains all of the conversions that exist for all codes. This also means that we can be internally consistent with country codes and not have to rely on third-party packages such as countrycode
to perform conversions for us.37
The fact that the API is not managed by someone with links to FAO is not a problem in itself, but it is suboptimal for the purposes of solving technical issues that depend on edge cases or implementation details of FAOstat. It further means that if FAO were to wish to promote this package to third parties, it wouldn’t be able to decide any priorities in regards to its development in terms of bug fixes or new features.
The author has worked as a consultant for FAO and is currently in contact with the previous maintainers of FAOSTAT
as well as developers who are currently working on the FAOstat API. The transfer of maintainership is however, as of the time of writing, incomplete.
Obsolete dependencies result from three major sources. The packages attached to each point are dependencies that have been removed from FAOSTAT
38:
ggplot2 (>= 0.9.3)
plyr (>= 1.7.1)
dplyr
, FAOSTAT
also depends on data.table
which covers many of the same data manipulations tasks.39RJSONIO (>= 0.96-0)
httr
which we are using now for requests uses the more modern jsonlite
classInt (>= 0.1-19)
labeling (>= 0.1)
The use of unit tests ensured there was no regression after removing these dependencies. The origin of these dependencies was difficult to track down as they were not added over time. Rather, they were all added in the first commit of the project.40 The conventional wisdom is to “Commit early, commit often.” This makes these changes granular, so it is easier to tease apart the individual changes in any commit (Eloe (2021)).
The two most important developers of the FAOSTAT
package are the package author, Michael Kao and the current maintainer, Paul Rougieux.
Paul Rougieux has been very helpful in guiding the author’s proposed changes to the package - making excellent suggestions in regards to code style and formatting as well as documentation and general usability.
An interview with the Michael Kao41 confirmed that a number of of unusual functions were due to its previous linkage with the Statistical Yearbook and that the paper attached to the package had never been published and suggested that it should be tidied up and published in future.
The existing functions to be refactored to read data from the new API. FAOSTAT
2.3.0 only had these core functions in scope.42 The most core functions include:
getFAO
43 - The heart of the package, pulls a custom slice of data from FAOstat. Renamed to read_fao
FAOsearch
44 - Allows a user to find the dataset they’re looking for using the directory in FAOstat. Renamed to search_fao
translateCountryCode
45 - Translates country codes between formats. Renamed to translate_countrycodes
download_faostat_bulk
- Downloads bulk zip files - We opted not to change the API of this function as it has been a backbone of the package since the shutdown of the FAOstat API.The functions were all modified for FAOSTAT
2.3.0, but it remained to modify all of the remaining functions so they had a consistent naming. The functions were mapped to more closely match the API. Each API object has a subset of functions which are similar between different objects. They have the following pattern:
list_groups
list_domains
- Lists domains in a particular group. To list all domains, there is also a list_domains_all
.read_fao
- an exception to the usual rule as it’s effectively the main function of theread_domain_dimension
read_dimension_code
read_dimension_metadata
ping
search_domain
download_faostat_bulk
- we have opted not to change the name for now, but we may add read_bulk
and read_bulk_metadata
.Where possible, we have retained the old names of functions and simply redirected them to point to the new ones.
It was intended that the bulk zip download files remained intact, as they were the most functional part of the package. However, it depends on FAOsearch
/search_fao
which was changed as part of the 2.3.0 release. As a result, it was necessary to refactor some of the bulk download code to restore functionality.
Regardless of the level of care in ensuring that only functions that don’t serve a purpose to most users are removed, there may still be cases where a user has their workflow broken by this removal. We have opted to mark these functions as “defunct”. They are not exported and attempting to use them gives the following error:
Error: 'function' is defunct.
See help("Defunct")
Their help page is still intact which greatly assists in recovering from such an error and also allows advanced users to examine the defunct code and modify it to suit their needs as required.
One of the drawbacks of moving from internal data to pulling from the API is the need to read in data every time a lookup table or other piece of reference data is required. This slows down scripts as it is forced to wait for a reply from the FAOstat server. To mitigate that, caching was implemented. 46 Now instead of pulling every time, the server is polled only once and the response is kept and referred to for subsequent queries.
When using the package, it was found that certain functionalities were immediately necessary. It was nigh-impossible to practically make a request for data without knowing what datasets were available and what their column names were. As a last minute addition, functions to perform these tasks were slipped into the 2.3.0 release.47
When making changes to a codebase it is important to make tests to assure that the function works as intended but also to report when the function breaks as a result of changes to it or its dependencies. This package uses testthat
for its tests (Wickham 2011).
Making tests is repetitive work however and many tests are structured the same way. This sort of repetitive intellectual work is perfect for an AI. The author used ChatGPT to help generate tests.48 It was supplied with the function code and responded with valid tests from which relevant ones were selected.
The package is documented using roxygen2
(Wickham et al. 2022) which allows documentation and code to be kept in the same file. Documentation has been particularly important for this project, specifically examples. As a lot of information has to be supplied to a function to get data in terms of codes, it is important that users have a clear idea of what is required as incorrect codes can result in cryptic empty responses from the server.
API wrappers like the FAOSTAT
package require a substantial amount of work and are very specific to a particular service. Furthermore, there’s no standard to how they should be created, meaning that even two wrappers to the same API have completely different approaches. Another approach is to have a standardised “wrapper” for the user and develop multiple config files to each allow connecting to a different API (Wu et al. 2023).
The FAOSTAT
package has never been officially published in a way that has been fully sponsored by FAO. However, it is important to widely publicise it to encourage use. The following avenues are available:
FAOSTAT
organisation
This project has been funded by the Food and Agriculture Organization of the United Nations and the author is grateful for their help in reviving it.
Food and Agriculture Organization of the United Nations↩︎
Food and Agriculture Organization Corporate Statistical Database↩︎
For the purposes of clarity, this document will use the style FAOSTAT
in monospace for the R package3 and “FAOstat” for the statistical platform↩︎
Application Programming Interface↩︎
This discrepancy has been fixed as of 2023-03-10↩︎
Application Programming Interface↩︎
REpresentative State Transfer↩︎
HyperText Transfer Protocol↩︎
There are other verbs (HEAD, CONNECT, OPTIONS, TRACE and PATCH), but they are of much lower importance↩︎
Simple Object Access Protocol↩︎
Food and Agriculture Organization of the United Nations↩︎
United Nations↩︎
For more details, the UN website has an excellent organigram↩︎
Food and Agriculture Organization Corporate Statistical Database↩︎
Highchart is a charting library allowing the construction of interactive graphs of multiple different kinds↩︎
For example, China may be just the mainland or may include Taiwan (Chinese Taipei), Hong Kong and Macao↩︎
Statistical Year Book↩︎
The author has no insight into the current production of the SYB, but they are still being produced and can be found on the FAO website↩︎
Personal communication, 2024↩︎
For a full description of the status of individual issues, please see the GitLab issue #20 Remove functions linked to defunct uses of FAOSTAT↩︎
For further details about FAO and how it handles country identification, see FAO’s NOCS database↩︎
Originally hosted at faostat3.fao.org↩︎
This was completed in FAOSTAT
2.3.0↩︎
For a full set of changes expected for FAOSTAT
3.0.0, see the GitLab milestone page↩︎
Comprehensive R Archive Network, the centralised repository where the majority of R users download R packages↩︎
Please see GitLab Milestone 2.3.0 for a full list of related issues↩︎
For more information, see the International Standards Organization glossary on country codes↩︎
JSON:API is a standard way to describe the structure of an API as a single json document↩︎
Personal communication, 2023↩︎
A full set of this examination is found in GitLab issue #23↩︎
countrycode
would only work, as the name suggests, for country codes and wouldn’t be helpful for other kinds of codes such as Commodity codes↩︎
And is, in the opinion of the author, a generally superior package↩︎
Personal Communication, 2023↩︎
A full list of functions to be refactored or discarded is in GitLab issue #20↩︎
ChatGPTv3 conversation dated 2023-03-28↩︎
@online{campbell2024,
author = {Campbell, Sebastian},
title = {`FAOSTAT` 3.0},
date = {2024-03-08},
langid = {en}
}