Skip to content

Commit

Permalink
Merge pull request #93 from bcgov/show-files
Browse files Browse the repository at this point in the history
Show files
  • Loading branch information
ateucher authored Jan 28, 2021
2 parents d206f76 + de5f359 commit b77ce28
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# bcmaps (development version)

* New helper function `show_cached_files()` to show the files that you have cached (and how much space they're taking up on your computer). (#92, #93)

# bcmaps 1.0.1
- When R version is >= 4.0, bcmaps will use `tools::R_user_dir("bcmaps",
"cache")` to determine the cache directory, while when R version is < 4.0, it
Expand Down
53 changes: 43 additions & 10 deletions R/cache-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' from the cache. Defaults to deleting all files pausing for permission from user. If a subset
#' of files are specified, the files are immediately deleted.
#'
#' @return A logical of whether the file(s) were successful deleted
#' @return `delete_cache()`: A logical of whether the file(s) were successful deleted
#'
#' @export
#' @examples
Expand All @@ -34,38 +34,71 @@
#' }

delete_cache <- function(files_to_delete = NULL) {
files <- show_cached_files()
files <- list_cached_files()

if(is.null(files_to_delete)) {
if (is.null(files_to_delete)) {
if (interactive()) {
ans <- ask(paste0("These are all the files you currently have stored locally: \n",
paste0(files, collapse = "\n"),
"\n \nAre you sure you want to delete all these files: \n"))
if(!ans) stop("Phewf! Glad you re-considered.", call. = FALSE)
if (!ans) stop("Phewf! Glad you re-considered.", call. = FALSE)
}
} else {
files <- file.path(data_dir(), add_rds_suffix(files_to_delete))
files <- file.path(data_dir(), files_to_delete)
files <- add_rds_suffix(files)
}

unlink(files)
unlink(files, recursive = TRUE)

## return FALSE if any file isn't deleted
invisible(all(!file.exists(files)))


}

#' Show the files you have in your cache
#'
#' @rdname delete_cache
#'
#' @export
#' @return `show_cached_files()`: a data.frame with the columns:
#' - `file`, the name of the file,
#' - `size_MB`, file size in MB,
#' - `is_dir`, is it a directory
#' - `modified`, date and time last modified
#'

#' @export
show_cached_files <- function() {
file.path(list.files(data_dir(), full.names = TRUE))
files <- tidy_files(list_cached_files())
if (any(grepl("cded", files$file))) {
cded_files <- tidy_files(list_cded_files())
total_cded_size <- sum(cded_files$size_MB, na.rm = TRUE)
files$size_MB[grepl("cded", files$file)] <- total_cded_size
}
files
}

tidy_files <- function(files) {
tbl <- file.info(files)
tbl$file <- rownames(tbl)
rownames(tbl) <- NULL
tbl$size_MB <- tbl$size / 1e6
tbl$modified <- tbl$mtime
tbl$is_dir <- tbl$isdir
tbl <- tbl[, c("file", "is_dir", "size_MB", "modified")]
class(tbl) <- c("tbl_df", "tbl", "data.frame")
tbl
}

list_cached_files <- function() {
list.files(data_dir(), full.names = TRUE)
}

list_cded_files <- function() {
list.files(file.path(data_dir(), "cded"), full.names = TRUE, recursive = TRUE)
}

add_rds_suffix <- function(x) {
exts <- tools::file_ext(x)
ifelse(exts == "" , paste0(x, ".rds"), x)
ifelse(exts == "" & !dir.exists(x), paste0(x, ".rds"), x)
}

1 change: 1 addition & 0 deletions R/cded.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ get_mapsheet_tiles <- function(mapsheet, dir, check_tiles = TRUE) {


if (length(tiles_need)) {
dir.create(dirname(tiles_need[1]), showWarnings = FALSE)
message("Fetching tiles for mapsheet ", mapsheet)
# download the ones we need
pb <- progress::progress_bar$new(
Expand Down
12 changes: 11 additions & 1 deletion man/delete_cache.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/testthat/test-cache-utils.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
context("test-cache-utils")

test_that("show_cached_files works", {
skip_on_cran()
skip_if_offline()

datadir <- file.path(tempdir(), "bcmaps")
withr::local_options(list("bcmaps.data_dir" = datadir))
cded_dir <- file.path(data_dir(), "cded")
dir.create(cded_dir, recursive = TRUE, showWarnings = FALSE)

expect_is(airzones(ask = FALSE, force = TRUE), "sf")
get_mapsheet_tiles("82o", cded_dir)
cache_info <- show_cached_files()
expect_s3_class(cache_info, "data.frame")
expect_equal(names(cache_info), c("file", "is_dir", "size_MB", "modified"))
expect_true(sum(cache_info$size_MB) > 10)
expect_equal(cache_info$is_dir, c(FALSE, TRUE))
})

test_that("the cache is deleted",{
skip_on_cran()
skip_if_offline()
expect_is(airzones(ask = FALSE, force = TRUE), "sf")
expect_true(delete_cache("airzones"))
expect_is(airzones(ask = FALSE, force = TRUE), "sf")
Expand Down

0 comments on commit b77ce28

Please sign in to comment.