R/Save_apikey.R

Defines functions save_apikey

Documented in save_apikey

#' Saves the apikey to project .Renviron for future use
#'
#' @param apikey is the APIkey obtained from Newsapi.org
#' @importFrom usethis edit_r_environ
#' @importFrom assertthat assert_that
#' @export

save_apikey <- function(apikey = NULL) {
  usethis::edit_r_environ('project')
  assertthat::assert_that(file.exists(".Renviron"))
  renv_contents <- readLines(".Renviron")
  assertthat::assert_that(!is.null(apikey))
  file_changed <- F
  
  if (!is.null(apikey)) {
    assertthat::assert_that(is.character(apikey))
    apikey_exists <- any(grepl("apikey", renv_contents))
    if (!apikey_exists) {
      str <- sprintf('\napikey = \"%s\"\n', apikey)
      cat(str, file = ".Renviron", append = TRUE)
      file_changed <- T
    } else {
      warning(".Renviron already contains an apikey; no file change was made.\n")
    }
  }
  
  if (file_changed) {
    source(".Renviron")
  }
}
eshitazaman/stat585-project documentation built on May 17, 2022, 7:41 a.m.