R/refresh-renviron.R

Defines functions refresh_renviron

Documented in refresh_renviron

#' Refreshes in-memory environment variables (if it exists)
#'
#' Equivanent to: `readRenviron("~/.Renviron")`
#'
#' @export
refresh_renviron <- function() {

  renv <- path.expand("~/.Renviron")

  curr_env <- tidyr::gather(tibble::as_tibble(as.list(Sys.getenv())), env_var, value)

  if (file.exists(renv)) readRenviron(renv)

  new_env <- tidyr::gather(tibble::as_tibble(as.list(Sys.getenv())), env_var, new_value)

  both_env <- full_join(curr_env, new_env, by="env_var")

  changed <- filter(both_env, value != new_value)
  if (nrow(changed) > 0) {
    cat(sprintf("The following environment variables were changed:\n%s",
                sprintf("  - %s\n", changed$env_var)))
  }

}
hrbrmstr/hrbraddins documentation built on Oct. 20, 2019, 7:43 a.m.