Skip to content

Commit

Permalink
fix: now check_internet returning invisibly
Browse files Browse the repository at this point in the history
  • Loading branch information
c1au6i0 committed Dec 19, 2024
1 parent efeb81d commit 927ce35
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 125 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: extractox
Title: Extract Tox Info from Various Databases
Version: 0.1.1
Version: 0.1.9001
Authors@R: c(
person("Claudio", "Zanettini", , "[email protected]", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0001-5043-8033")),
Expand Down
67 changes: 67 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,75 @@ check_internet <- function(verbose = TRUE) {
}
out <- TRUE
}
invisible(out)
}







#' Save an object to the package cache
#'
#' This function saves an R object to the cache directory of the `extractox` package
#' using the `.rds` file format. If a file with the same name already exists in the cache,
#' it will be overwritten.
#'
#' @param obj Any R object to be saved.
#' @param file_name A character string specifying the name of the file (without extension).
#' @param verbose A logical value indicating whether to print detailed messages. Default is FALSE.
#' @return Invisibly returns the full path of the saved file.
#' @details The cache directory is determined using [tools::R_user_dir()] with the `cache` subdirectory
#' for the `extractox` package. If the directory does not exist, it is created automatically.
#' The function will overwrite any existing file with the same name.
#' @noRd
save_to_cache <- function(obj, file_name, verbose = FALSE) {
cache_dir <- tools::R_user_dir("extractox", which = "cache")

if (!dir.exists(cache_dir)) {
dir.create(cache_dir, recursive = TRUE)
}

file_path <- file.path(cache_dir, paste0(file_name, ".rds"))

if (all(file.exists(file_path), verbose)){
cli::cli_alert_info("Overwriting cache.")
}

saveRDS(obj, file_path)

invisible(file_path)
}

#' Read an object from the package cache
#'
#' This function reads an R object from the cache directory of the `extractox` package
#' using the `.rds` file format. If the file does not exist, it stops.
#'
#' @param file_name A character string specifying the name of the file (without extension).
#' @param verbose A logical value indicating whether to print detailed messages. Default is FALSE.
#' @return The R object read from the cache, or NULL if the file does not exist.
#' @details The cache directory is determined using [tools::R_user_dir()] with the `cache` subdirectory
#' for the `extractox` package. If the file does not exist, a message is printed if verbose is TRUE.
#' @noRd
read_from_cache <- function(file_name, verbose = FALSE) {
cache_dir <- tools::R_user_dir("extractox", which = "cache")
file_path <- file.path(cache_dir, paste0(file_name, ".rds"))

if (file.exists(file_path)) {
out <- readRDS(file_path)
if (verbose) {
cli::cli_alert_success("Successfully read from cache.")
}
} else {
cli::cli_abort("File not found in cache.")
}

out
}


#' Selection of assays of iris

#' @keywords internal
Expand Down
Loading

0 comments on commit 927ce35

Please sign in to comment.