R/get-file.R

Defines functions get_file_xlsx get_file_csv get_file_jpg get_file_json

Documented in get_file_csv get_file_jpg get_file_json get_file_xlsx

#-----------------------------------------------------------------------------#
# FUNCTIONS TO GET FILE FOR A GIVEN URL FOR DIFFERENT FORMAT
#-----------------------------------------------------------------------------#

#' Get xlsx format file
#'
#' @param url the url of the file
#' @param path the directory where the file should be downloaded to
#' @param silient whether or not silent message
#'
#' @export
#'
get_file_xlsx <- function(url, path = ".", silent = TRUE) {
  path <- file.path(path, basename(url))
  download.file(url, path, quiet = TRUE)
  if (!silent) cat(sprintf("Downloaded to %s\n", path))
  path
}


#' Get csv format file
#'
#' @param url the url of the file
#' @param path the directory where the file should be save, if NULL it
#' will not save
#' @param silient whether or not silent message
#'
#' @return it returns a data frame read from the csv file
#'
#' @examples
#' data <- get_file_csv("http://www.chp.gov.hk/files/misc/statistics-2017-vss-eng.csv")
#'
#' @export
#'
get_file_csv <- function(url, path = NULL, col_names = TRUE, col_types = NULL, silent = TRUE) {
  require(readr)
  if (is.null(path)) {
    path <- file.path(tempdir(), basename(url))
    on.exit(unlink(path))
  } else {
    path <- file.path(path, basename(url))
  }
  download.file(url, path, quiet = TRUE)
  if (!silent) cat(sprintf("Downloaded to %s\n", path))
  data <- read_csv(path, col_name = col_names, col_types = col_types)
  data
}

#' Get jpg format file
#'
#' @param url the url of the file
#' @param path the directory to save the file
#' @param view if TRUE the the file is viewed after download
#' @param silient whether or not silent message
#'
#' @examples
#' get_file_jpg("http://www.toothclub.gov.hk/chi/jpg/psi/P041.jpg")
#'
#' @export
#'
get_file_jpg <- function(url, path = ".", view = TRUE, silent = TRUE) {
  path <- file.path(path, basename(url))
  download.file(url, path, quiet = TRUE)
  if (!silent) cat(sprintf("Downloaded to %s\n", path))
  if (view) {
    viewer <- getOption("viewer")
    viewer(path)
  }
}

#' Get json format file
#'
#' @param url the url of the file
#' @param path the directory where the file should be save, if NULL it
#' will not save
#' @param silient whether or not silent message
#'
#' @return it returns a data read from the json file
#'
#' @examples
#' data <- get_file_csv("http://www.ha.org.hk/opendata/ipdpdd-en.json")
#'
#' @export
#'
get_file_json <- function(url, path = NULL, silent = TRUE) {
  if (is.null(path)) {
    path <- file.path(tempdir(), basename(url))
    on.exit(unlink(path))
  } else {
    path <- file.path(path, basename(url))
  }
  download.file(url, path, quiet = TRUE)
  if (!silent) cat(sprintf("Downloaded to %s\n", path))
  data <- jsonlite::fromJSON(path)
  data
}
XiangdongGu/hkdata documentation built on Aug. 3, 2019, 6:18 p.m.