Skip to content

Instantly share code, notes, and snippets.

@fbiga
Last active January 24, 2020 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fbiga/0f94080bc9e645184a4368639b81d35a to your computer and use it in GitHub Desktop.
Save fbiga/0f94080bc9e645184a4368639b81d35a to your computer and use it in GitHub Desktop.
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 <- "http://fenixservices.fao.org/faostat/static/bulkdownloads/"
food_files <- c("CommodityBalances_Crops_E_All_Data.zip",
"CommodityBalances_LivestockFish_E_All_Data.zip")
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_fao(food_files[1])
# Load all files
for(f in food_files){
print(f)
load_fao(f)
}
# 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("CommodityBalances_Crops_E_All_Data.zip")
saveRDS(Crops_E,"raw_data/Crops_E.rds")
LivestockFish_E <- read_fao("CommodityBalances_LivestockFish_E_All_Data.zip")
saveRDS(LivestockFish_E, "raw_data/LivestockFish_E.rds")
@paulrougieux
Copy link

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

@paulrougieux
Copy link

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

Example use:

url_forestry <- "http://fenixservices.fao.org/faostat/static/bulkdownloads/Forestry_E_All_Data_(Normalized).zip"
download_faostat_bulk(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