R/utils.R

Defines functions ask_for_confirmation strip_ext download_with_retries get_os os_is_macos os_is_windows cloudstan_url

cloudstan_url <- function(path = "") {
  paste0("https://cloudstan.hercog.si", path)
}

os_is_windows <- function() {
  isTRUE(.Platform$OS.type == "windows")
}

os_is_macos <- function() {
  isTRUE(Sys.info()[["sysname"]] == "Darwin")
}

get_os <- function() {
  if(os_is_windows()) {
    "windows"
  } else if(os_is_macos()) {
    "darwin"
  } else{
    "linux"
  }
}

download_with_retries <- function(download_url,
                                  destination_file,
                                  retries = 5,
                                  pause_sec = 5,
                                  quiet = TRUE) {
  download_rc <- 1
  while (retries > 0 && download_rc != 0) {
    try(
      suppressWarnings(
        download_rc <- utils::download.file(url = download_url,
                                            destfile = destination_file,
                                            quiet = quiet,
                                            method = "curl")
      ),
      silent = TRUE
    )
    if (download_rc != 0) {
      Sys.sleep(pause_sec)
    }
    retries <- retries - 1
  }
  download_rc == 0
}

strip_ext <- function(file) {
  tools::file_path_sans_ext(file)
}

ask_for_confirmation <- function(msg = "Are you sure you want to proceed?") {
  readline(paste(msg, "(y/n): ")) == 'y'
}
uroshercog/cloudstan-r documentation built on Dec. 23, 2021, 2:03 p.m.