Skip to content

Commit b77ce28

Browse files
authored
Merge pull request #93 from bcgov/show-files
Show files
2 parents d206f76 + de5f359 commit b77ce28

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# bcmaps (development version)
22

3+
* 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)
4+
35
# bcmaps 1.0.1
46
- When R version is >= 4.0, bcmaps will use `tools::R_user_dir("bcmaps",
57
"cache")` to determine the cache directory, while when R version is < 4.0, it

R/cache-utils.R

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#' from the cache. Defaults to deleting all files pausing for permission from user. If a subset
1919
#' of files are specified, the files are immediately deleted.
2020
#'
21-
#' @return A logical of whether the file(s) were successful deleted
21+
#' @return `delete_cache()`: A logical of whether the file(s) were successful deleted
2222
#'
2323
#' @export
2424
#' @examples
@@ -34,38 +34,71 @@
3434
#' }
3535

3636
delete_cache <- function(files_to_delete = NULL) {
37-
files <- show_cached_files()
37+
files <- list_cached_files()
3838

39-
if(is.null(files_to_delete)) {
39+
if (is.null(files_to_delete)) {
4040
if (interactive()) {
4141
ans <- ask(paste0("These are all the files you currently have stored locally: \n",
4242
paste0(files, collapse = "\n"),
4343
"\n \nAre you sure you want to delete all these files: \n"))
44-
if(!ans) stop("Phewf! Glad you re-considered.", call. = FALSE)
44+
if (!ans) stop("Phewf! Glad you re-considered.", call. = FALSE)
4545
}
4646
} else {
47-
files <- file.path(data_dir(), add_rds_suffix(files_to_delete))
47+
files <- file.path(data_dir(), files_to_delete)
48+
files <- add_rds_suffix(files)
4849
}
4950

50-
unlink(files)
51+
unlink(files, recursive = TRUE)
5152

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

5556

5657
}
5758

59+
#' Show the files you have in your cache
60+
#'
5861
#' @rdname delete_cache
5962
#'
60-
#' @export
63+
#' @return `show_cached_files()`: a data.frame with the columns:
64+
#' - `file`, the name of the file,
65+
#' - `size_MB`, file size in MB,
66+
#' - `is_dir`, is it a directory
67+
#' - `modified`, date and time last modified
6168
#'
62-
69+
#' @export
6370
show_cached_files <- function() {
64-
file.path(list.files(data_dir(), full.names = TRUE))
71+
files <- tidy_files(list_cached_files())
72+
if (any(grepl("cded", files$file))) {
73+
cded_files <- tidy_files(list_cded_files())
74+
total_cded_size <- sum(cded_files$size_MB, na.rm = TRUE)
75+
files$size_MB[grepl("cded", files$file)] <- total_cded_size
76+
}
77+
files
6578
}
6679

80+
tidy_files <- function(files) {
81+
tbl <- file.info(files)
82+
tbl$file <- rownames(tbl)
83+
rownames(tbl) <- NULL
84+
tbl$size_MB <- tbl$size / 1e6
85+
tbl$modified <- tbl$mtime
86+
tbl$is_dir <- tbl$isdir
87+
tbl <- tbl[, c("file", "is_dir", "size_MB", "modified")]
88+
class(tbl) <- c("tbl_df", "tbl", "data.frame")
89+
tbl
90+
}
91+
92+
list_cached_files <- function() {
93+
list.files(data_dir(), full.names = TRUE)
94+
}
95+
96+
list_cded_files <- function() {
97+
list.files(file.path(data_dir(), "cded"), full.names = TRUE, recursive = TRUE)
98+
}
6799

68100
add_rds_suffix <- function(x) {
69101
exts <- tools::file_ext(x)
70-
ifelse(exts == "" , paste0(x, ".rds"), x)
102+
ifelse(exts == "" & !dir.exists(x), paste0(x, ".rds"), x)
71103
}
104+

R/cded.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ get_mapsheet_tiles <- function(mapsheet, dir, check_tiles = TRUE) {
175175

176176

177177
if (length(tiles_need)) {
178+
dir.create(dirname(tiles_need[1]), showWarnings = FALSE)
178179
message("Fetching tiles for mapsheet ", mapsheet)
179180
# download the ones we need
180181
pb <- progress::progress_bar$new(

man/delete_cache.Rd

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-cache-utils.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
context("test-cache-utils")
22

3+
test_that("show_cached_files works", {
4+
skip_on_cran()
5+
skip_if_offline()
6+
7+
datadir <- file.path(tempdir(), "bcmaps")
8+
withr::local_options(list("bcmaps.data_dir" = datadir))
9+
cded_dir <- file.path(data_dir(), "cded")
10+
dir.create(cded_dir, recursive = TRUE, showWarnings = FALSE)
11+
12+
expect_is(airzones(ask = FALSE, force = TRUE), "sf")
13+
get_mapsheet_tiles("82o", cded_dir)
14+
cache_info <- show_cached_files()
15+
expect_s3_class(cache_info, "data.frame")
16+
expect_equal(names(cache_info), c("file", "is_dir", "size_MB", "modified"))
17+
expect_true(sum(cache_info$size_MB) > 10)
18+
expect_equal(cache_info$is_dir, c(FALSE, TRUE))
19+
})
320

421
test_that("the cache is deleted",{
22+
skip_on_cran()
23+
skip_if_offline()
524
expect_is(airzones(ask = FALSE, force = TRUE), "sf")
625
expect_true(delete_cache("airzones"))
726
expect_is(airzones(ask = FALSE, force = TRUE), "sf")

0 commit comments

Comments
 (0)