Skip to content

Commit c26c7e9

Browse files
authored
Merge pull request #38 from explodecomputer/save-fix-III
Save fix iii
2 parents 4695441 + 911d5b1 commit c26c7e9

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

R/dictionary.r

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -52,37 +52,22 @@ retrieveDictionary <- function(name) {
5252
}
5353

5454

55-
saveDictionary <- function(name, dictionary, overwrite = 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)
59-
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")
55+
saveDictionary <- function(name, dictionary) {
56+
assign(name, dictionary, globals)
57+
#if (name == "current" || name == "useful")
58+
# combineDictionaries()
6359

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"
60+
path <- file.path(system.file(package="alspac"), "data")
61+
if (!file.exists(path)) {
62+
dir.create(path)
7363
}
74-
75-
if (!dir.exists(data_dir)) dir.create(data_dir, recursive = TRUE, showWarnings = FALSE)
76-
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)
64+
save(list=name,
65+
file=file.path(path, paste(name, "rdata", sep=".")),
66+
envir=globals)
8167
}
8268

8369

8470

85-
8671
#' Checks a dictionary
8772
#'
8873
#' Checks if all the files referred to in the dictionary
@@ -146,7 +131,7 @@ updateDictionaries <- function() {
146131
#'
147132
#' @export
148133
#' @return Data frame dictionary listing available variables.
149-
createDictionary <- function(datadir="Current", name= "current", quick=FALSE, sourcesFile = NULL) {
134+
createDictionary <- function(datadir="Current", name=NULL, quick=FALSE, sourcesFile = NULL) {
150135
stopifnot(datadir %in% c("Current", "../DataBuddy/DataRequests/Waiting Room"))
151136

152137
if(is.null(sourcesFile))
@@ -155,22 +140,24 @@ createDictionary <- function(datadir="Current", name= "current", quick=FALSE, so
155140
alspacdir <- options()$alspac_data_dir
156141
datadir <- file.path(alspacdir, datadir)
157142

158-
# ---list.files section with version handling ---
143+
# --- NEW list.files section with version handling ---
159144
files <- list.files(datadir,
160145
pattern="dta$",
161146
full.names=TRUE,
162147
recursive=TRUE,
163148
ignore.case=TRUE)
164149

150+
# Extract base name and version (assuming suffix like _1a, _2b etc.)
165151
fnames <- basename(files)
166152
parts <- sub("\\.dta$", "", fnames) # drop extension
167153
base <- sub("_[0-9]+[a-zA-Z]$", "", parts) # everything before version
168154
vers <- sub(".*_", "", parts) # the version part (e.g., 1a, 2b)
169155

156+
# Split numeric and letter parts
170157
num <- suppressWarnings(as.integer(sub("([0-9]+).*", "\\1", vers)))
171158
let <- sub("[0-9]+", "", vers)
172159

173-
# Build table of file info
160+
# Build table of file info
174161
file_info <- data.frame(
175162
file = files,
176163
base = base,
@@ -179,12 +166,13 @@ createDictionary <- function(datadir="Current", name= "current", quick=FALSE, so
179166
stringsAsFactors = FALSE
180167
)
181168

169+
# Keep latest per base (highest number, then highest letter)
182170
latest_files <- file_info |>
183171
dplyr::group_by(base) |>
184172
dplyr::arrange(dplyr::desc(num), dplyr::desc(let)) |>
185173
dplyr::slice_head(n = 1) |>
186174
dplyr::pull(file)
187-
# --- END of new section ---
175+
# --- END of new section ---
188176

189177
dictionary <- parallel::mclapply(latest_files, function(file) {
190178
cat(date(), "loading", file, "\n")
@@ -199,18 +187,13 @@ createDictionary <- function(datadir="Current", name= "current", quick=FALSE, so
199187
})
200188
}) %>% dplyr::bind_rows()
201189

202-
203190
dictionary <- dictionary[which(dictionary$counts > 0),]
204191

205-
206-
## Add sources info
207192
dictionary <- addSourcesToDictionary(dictionary, sourcesFile)
208193

209-
## Assign in globals so retrieveDictionary() can find it
210-
assign(name, dictionary, globals)
211-
212-
## Save using saveDictionary() function
213-
saveDictionary(name, dictionary)
194+
if (!is.null(name)) {
195+
saveDictionary(name, dictionary)
196+
}
214197

215198
invisible(dictionary)
216199
}

inst/data/current.rda

1.52 MB
Binary file not shown.

0 commit comments

Comments
 (0)