R/zzz.R

Defines functions .onUnload .onLoad clear_venv_path load_venv_path save_venv_path config_dir

Documented in clear_venv_path config_dir load_venv_path save_venv_path

#' @importFrom curl form_file
#' @importFrom httr2 request req_body_json req_body_multipart req_error
#'   req_perform req_timeout req_url_path req_url_path_append resp_body_json
#'   resp_status
#' @importFrom processx process
#' @importFrom rlang abort inform is_installed
#' @keywords internal
"_PACKAGE"

# Private environment shared across the package
.pkg_env <- new.env(parent = emptyenv())

#' Path to the package config directory
#' @return Character path to the config directory.
#' @keywords internal
config_dir <- function() {
  tools::R_user_dir("rMIDAS2", "config")
}

#' Save the virtualenv path to persistent config
#' @param path Character path to save.
#' @return No return value, called for side effects.
#' @keywords internal
save_venv_path <- function(path) {
  dir <- config_dir()
  if (!dir.exists(dir)) dir.create(dir, recursive = TRUE)
  writeLines(normalizePath(path, mustWork = FALSE), file.path(dir, "venv_path"))
}

#' Load the saved virtualenv path (or NULL)
#' @return Character path or `NULL`.
#' @keywords internal
load_venv_path <- function() {
  f <- file.path(config_dir(), "venv_path")
  if (file.exists(f)) readLines(f, n = 1L) else NULL
}

#' Remove the saved virtualenv path
#' @return No return value, called for side effects.
#' @keywords internal
clear_venv_path <- function() {
  f <- file.path(config_dir(), "venv_path")
  if (file.exists(f)) unlink(f)
  .pkg_env$venv <- NULL
}

.onLoad <- function(libname, pkgname) {
  .pkg_env$process <- NULL
  .pkg_env$port <- NULL
  .pkg_env$base_url <- NULL
  .pkg_env$venv <- load_venv_path()
}

.onUnload <- function(libpath) {
  try(stop_server(), silent = TRUE)
}

Try the rMIDAS2 package in your browser

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

rMIDAS2 documentation built on March 12, 2026, 9:07 a.m.