Skip to content

Commit cd08881

Browse files
committed
save-fix
-remove copy to /inst -function to saving in dev mode and install package mode
1 parent 0d0caed commit cd08881

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

R/dictionary.r

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,36 @@ retrieveDictionary <- function(name) {
5353

5454

5555
saveDictionary <- function(name, dictionary, overwrite = TRUE) {
56-
# Save in the globals environment
57-
58-
assign(name, dictionary, globals)
59-
60-
# -------------------------------
61-
# 1️⃣ Save CRAN/GitHub-compliant copy (/data/)
62-
# -------------------------------
63-
64-
data_path <- file.path(getwd(), "data")
65-
66-
if (!dir.exists(data_path)) dir.create(data_path, recursive = TRUE)
56+
# Put the dictionary into memory
57+
gl <- if (exists("globals", envir = .GlobalEnv)) get("globals", envir = .GlobalEnv) else .GlobalEnv
58+
assign(name, dictionary, envir = gl)
6759

68-
save(list = name,
69-
file = file.path(data_path, paste0(name, ".rda")),
70-
envir = globals)
60+
# Detect package root (dev) and install path
61+
pkg_root <- tryCatch(rprojroot::find_root(rprojroot::has_dir("R")), error = function(e) getwd())
62+
pkg_inst_path <- system.file(package = "alspac")
7163

64+
# Decide where to save
65+
if (nzchar(pkg_inst_path) && !grepl("Git|dev|Documents", pkg_inst_path, ignore.case = TRUE)) {
66+
# Installed package
67+
data_dir <- file.path(pkg_inst_path, "data")
68+
mode <- "installed"
69+
} else {
70+
# Development mode
71+
data_dir <- file.path(pkg_root, "data")
72+
mode <- "dev"
73+
}
7274

73-
# -------------------------------
74-
# 2️⃣ Save dev copy (/inst/data/) for load_all() testing
75-
# -------------------------------
76-
inst_path <- file.path(getwd(), "inst", "data")
77-
if (!dir.exists(inst_path)) dir.create(inst_path, recursive = TRUE)
75+
if (!dir.exists(data_dir)) dir.create(data_dir, recursive = TRUE, showWarnings = FALSE)
7876

79-
save(list = name,
80-
file = file.path(inst_path, paste0(name, ".rdata")),
81-
envir = globals)
82-
message("Dictionary '", name, "' saved to:\n",
83-
" - ", file.path(data_path, paste0(name, ".rda")), "\n",
84-
" - ", file.path(inst_path, paste0(name, ".rdata")))
77+
save_path <- file.path(data_dir, paste0(name, ".rdata"))
78+
save(list = name, file = save_path, envir = gl)
79+
message("Dictionary '", name, "' saved to /data/ (", mode, "):\n ", save_path)
80+
invisible(save_path)
8581
}
8682

83+
84+
85+
8786
#' Checks a dictionary
8887
#'
8988
#' Checks if all the files referred to in the dictionary

inst/data/current.rdata

-1.52 MB
Binary file not shown.

0 commit comments

Comments
 (0)