R/mck_get.R

#' get market cap data
#'
#' @param year target year to get
#' @importFrom fs path path_temp
#' @importFrom utils download.file
#' @return a [tibble][tibble::tibble-package]
#' @export
mck_get <- function(year){
  root <- "https://raw.githubusercontent.com/mrchypark/marcapkor-data/master/mc"
  tar <- paste0(root, year, ".rds")
  temp <- fs::path(fs::path_temp(),paste0("mc",year,".rds"))
  download.file(tar, destfile = temp, method = "curl")
  res <- readRDS(temp)
  return(res)
}

#' get market cap all year data
#'
#' @importFrom fs path path_temp
#' @importFrom xml2 read_html
#' @importFrom rvest html_nodes html_text
#' @importFrom utils download.file
#' @importFrom purrr map map_dfr
#' @return a [tibble][tibble::tibble-package]
#' @export
mck_get_all <- function(){

  "https://github.com/mrchypark/marcapkor-data" %>%
    xml2::read_html() %>%
    rvest::html_nodes("td.content span a") %>%
    rvest::html_text() -> data_list

  root <- "https://raw.githubusercontent.com/mrchypark/marcapkor-data/master/"

  data_list %>%
    purrr::map(
      ~ download.file(
      paste0(root,.x),
      fs::path(fs::path_temp(),.x),
      method = "curl"
      )
    )

  data_list %>%
    purrr::map_dfr(~ readRDS(fs::path(fs::path_temp(), .x))) -> res
  return(res)
}
mrchypark/marcapkor documentation built on May 27, 2019, 9:56 a.m.