/
isoread_rda.R
58 lines (49 loc) · 2.73 KB
/
isoread_rda.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# read cached isoreader files
# @param ds the data structure to fill
# @param custom reader options - none needed
iso_read_rda <- function(ds, options = list()) {
# safety checks
if(!iso_is_file(ds)) stop("data structure must be an iso_file", call. = FALSE)
# general deprecation warning
log_warning("R Data Archives (.rda) are deprecated in favor of R Data Storage (.rds) files, please re-save your isofiles using the new iso_save() function.")
# load rda file
if (exists("iso_files", inherits = FALSE)) rm("iso_files")
if (exists("isofiles", inherits = FALSE)) rm("isofiles") # backwards compatibility
load(get_ds_file_path(ds))
# make sure object in file was loaded properly
if (exists("isofiles", inherits = FALSE)) {
if (!is.list(isofiles)) isofiles <- as.list(isofiles)
is_old_isofile <- map_lgl(isofiles, ~is(.x, "isofile"))
isofiles[is_old_isofile] <- map(isofiles[is_old_isofile], function(isofile) {
class(isofile) <- class(isofile) %>% { .[.!="isofile"] } %>% c("iso_file")
return(isofile)
})
iso_files <- iso_as_file_list(isofiles)
}
if (!exists("iso_files", inherits = FALSE) || !(iso_is_object(iso_files)))
stop("R Data Archive did not contain iso_file data", call. = FALSE)
iso_files <- iso_as_file_list(iso_files)
# make sure all are the appropriate classes
if (!all(ok <- lapply(iso_files, class) %>% sapply(identical, class(ds))))
sprintf("Mismatched file types, expected '%s' but encountered '%s'",
str_c(class(ds)[1]), str_c(iso_files[!ok] %>% sapply(function(i) class(i)[1]) %>% unique(), collapse = ", ")) %>%
stop(call. = FALSE)
# information
if (!default("quiet")) {
sprintf("loaded data for %d data files from R Data Archive - checking loaded files for content consistency...", length(iso_files)) %>%
log_message()
}
# check for version warning
versions <- map(iso_files, `[[`, "version")
ok_version <- map_lgl(versions, same_as_isoreader_version, packageVersion("isoreader"))
if (any(!ok_version)) {
messages <- sprintf("file created by a different version of the isoreader package (%s)", map_chr(versions[!ok_version], as.character))
iso_files[!ok_version] <- map2(iso_files[!ok_version], messages, register_warning, func = "iso_read_rda", warn = FALSE)
}
if (any(!ok_version)) {
sprintf("version mismatch - %.0f of the %.0f data files stored in the R Data Archive ('%s') were created by a different version of the isoreader package. This may lead to processing problems.", sum(!ok_version), length(iso_files), ds$file_info$file_id) %>%
log_warning()
log_warning("Consider re-reading the original data files using the 'iso_reread_files()' or 'iso_reread_storage()' function.")
}
return(iso_files)
}