Skip to content

Instantly share code, notes, and snippets.



Last active Jan 24, 2020
What would you like to do?
Load data from faostat bulk download into R data frames

Copyright 2019 fbiga Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

base_url_FAO <- ""
food_files <- c("",
load_fao <- function(file_name, base_url = base_url_FAO, data_folder="raw_data"){
download.file(paste0(base_url, file_name),
file.path(data_folder, file_name))
# Load one file
# Load all files
for(f in food_files){
# Function to read zipped files and return a data frame
#' @param file_name character file name
#' @param data_folder character path to the data
#' @return data frame of fao data
read_fao <- function(file_name, data_folder="raw_data"){
csv_file_name <- gsub(".zip$",".csv", file_name)
read.csv(unz(file.path(data_folder, file_name), csv_file_name),
stringsAsFactors = FALSE)
# Read files and assign them to data frames
Crops_E <- read_fao("")
LivestockFish_E <- read_fao("")
saveRDS(LivestockFish_E, "raw_data/LivestockFish_E.rds")

This comment has been minimized.

Copy link

@paulrougieux paulrougieux commented Oct 7, 2019

Nice @fbiga can you please add a licence, such as MIT?


This comment has been minimized.

Copy link

@paulrougieux paulrougieux commented Jan 24, 2020

I simplified the functions, reduced the number of arguments in this gist.

Example use:

url_forestry <- ""
forestry_e_all_data <- read_faostat_bulk("data_raw/Forestry_E_All_Data_(Normalized).zip")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment