R/utils.R

Defines functions as_city_code as_pref_code get_content str_to_camelcase str_to_snakecase compact_query commas0 commas big_mark

big_mark <- function(x) {
  mark <- if (identical(getOption("OutDec"), ",")) {
    "."
  } else {
    ","
  }

  formatC(x,
          big.mark = mark)
}

commas <- function(...) {
  stringr::str_c(...,
                 collapse = ", ")
}

commas0 <- function(...) {
  stringr::str_c(...,
                 collapse = ",")
}

compact_query <- function(...) {
  dots_list(...,
            .named = TRUE,
            .homonyms = "first") |>
    purrr::compact()
}

str_to_snakecase <- function(string) {
  string |>
    stringr::str_split("(?=[[:upper:]])") |>
    purrr::map_chr(function(string) {
      string |>
        stringr::str_to_lower() |>
        stringr::str_c(collapse = "_")
    }) |>
    stringr::str_remove("^_")
}

str_to_camelcase <- function(string) {
  string |>
    stringr::str_split("_") |>
    purrr::map_chr(\(x) {
      exec(stringr::str_c, x[[1L]], !!!stringr::str_to_sentence(x[-1L]),
           collapse = "")
    })
}

get_content <- function(url = NULL, config = list(), ..., handle = NULL) {
  out <- httr::GET(url = url,
                   config = config, ...,
                   handle = handle)
  httr::stop_for_status(out)
  httr::content(out)
}



# Pref and city codes -----------------------------------------------------

as_pref_code <- function(x) {
  x |>
    stringr::str_pad(2L,
                     pad = "0")
}

as_city_code <- function(x) {
  x |>
    stringr::str_pad(5L,
                     pad = "0")
}



# Progress bar ------------------------------------------------------------

format_downloading <- "downloading [:bar] :percent"

Try the jpstat package in your browser

Any scripts or data that you put into this service are public.

jpstat documentation built on July 26, 2023, 6:02 p.m.