Skip to content

Instantly share code, notes, and snippets.

@paulrougieux
Forked from fbiga/License.md
Last active January 24, 2020 09:01
Show Gist options
  • Save paulrougieux/07c2926dc898a9d90c7df99037148d43 to your computer and use it in GitHub Desktop.
Save paulrougieux/07c2926dc898a9d90c7df99037148d43 to your computer and use it in GitHub Desktop.
Load data from faostat bulk download into R data frames
#' Download bulk data from the faostat website
#' http://www.fao.org/faostat/en/#data
#'
#' Note the files called "normalized" are in long format
#' with a year column instead of one column for each year.
#' The long format is preferable for data analysis.
#' @param url_bulk character url of the faostat bulk zip file to download
#' @param data_folder character path of the local folder where to download the data
#' @author Paul Rougieux
#' @examples
#' \dontrun{
#' # Load global forestry data in long format
#' url_forestry <- "http://fenixservices.fao.org/faostat/static/bulkdownloads/Forestry_E_All_Data_(Normalized).zip"
#' download_faostat_bulk(url_forestry)
#' }
#' @export
download_faostat_bulk <- function(url_bulk, data_folder="data_raw"){
file_name <- basename(url_bulk)
download.file(url_bulk, file.path(data_folder, file_name))
}
#' Function to read zipped files and return a data frame
#' Reads the main csv file within the archive.
#' The main file has the same name as the name of the archive.
#' Note: the zip archive might also contain metadata files about Flags and Symboles.
#' @param zip_file_name character name of the zip file to read
#' @return data frame of FAOSTAT data
#' @examples
#' \dontrun{
#' # Read a file then assign it to a data frame and save it as rds
#' forestry_e_all_data <- read_faostat_bulk("data_raw/Forestry_E_All_Data_(Normalized).zip")
#' saveRDS(forestry_e_all_data,"data_raw/forestry_e_all_data.rds")
#' }
#' @export
read_faostat_bulk <- function(zip_file_name){
# The main csv file shares the name of the archive
csv_file_name <- gsub(".zip$",".csv", basename(zip_file_name))
# Read the csv file within the zip file
df <- read.csv(unz(zip_file_name, csv_file_name), stringsAsFactors = FALSE)
return(df)
}

MIT

Copyright 2020 Paul Rougieux, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment