|
a |
|
b/R/utility_functions.R |
|
|
1 |
#' Retrieve path to a cached file. |
|
|
2 |
#' |
|
|
3 |
#' The function retrieves the path to a file that is cached in the archive |
|
|
4 |
#' directory. |
|
|
5 |
#' |
|
|
6 |
#' @param filename Name of the file. |
|
|
7 |
#' |
|
|
8 |
#' @return String containing the path to the file. |
|
|
9 |
archivePath <- function(filename) { |
|
|
10 |
ad <- archiveDir() |
|
|
11 |
filename <- paste0(filename, ".rds") |
|
|
12 |
|
|
|
13 |
return(file.path(ad, filename, fsep = .Platform$file.sep)) |
|
|
14 |
} |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
#' Retrieve the path to the cache directory. |
|
|
21 |
#' |
|
|
22 |
#' Retrieve the path to the cache directory for the multiGSEA package. |
|
|
23 |
#' Create the cache directory if need be. |
|
|
24 |
#' |
|
|
25 |
#' @return String containing the path to the cache directory. |
|
|
26 |
#' |
|
|
27 |
#' @importFrom rappdirs user_cache_dir |
|
|
28 |
archiveDir <- function() { |
|
|
29 |
ad <- rappdirs::user_cache_dir("multiGSEA") |
|
|
30 |
|
|
|
31 |
if (!file.exists(ad)) { |
|
|
32 |
if (!dir.create(ad, FALSE, TRUE)) { |
|
|
33 |
stop("An error occurred during creating the archive directory: ", ad, |
|
|
34 |
call. = FALSE |
|
|
35 |
) |
|
|
36 |
} |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
return(ad) |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
#' Read a local RDS file. |
|
|
47 |
#' |
|
|
48 |
#' Use the readRDS function to load the given file which should be in RDS |
|
|
49 |
#' format. |
|
|
50 |
#' |
|
|
51 |
#' @param filename Path to the file to be read. |
|
|
52 |
#' |
|
|
53 |
#' @return Content of file. |
|
|
54 |
#' |
|
|
55 |
#' @importFrom methods is |
|
|
56 |
loadLocal <- function(filename) { |
|
|
57 |
res <- try(readRDS(filename), silent = TRUE) |
|
|
58 |
|
|
|
59 |
if ("try-error" %in% methods::is(res)) { |
|
|
60 |
return(NULL) |
|
|
61 |
} else { |
|
|
62 |
return(res) |
|
|
63 |
} |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
|
|
|
67 |
#' Make a list of strings unique |
|
|
68 |
#' |
|
|
69 |
#' It might happen that there are duplicated strings in a list. With this |
|
|
70 |
#' function we will rename those duplicated entries in a way that we simply add |
|
|
71 |
#' the number of occurrences to the string. I.e., when the string foo occurs |
|
|
72 |
#' three times in a list, it will be renamed to foo_1, foo_2, and foo_3, |
|
|
73 |
#' respectively. |
|
|
74 |
#' |
|
|
75 |
#' @param names List of strings where duplicates should be renamed |
|
|
76 |
#' |
|
|
77 |
#' @return List where duplicates are renamed. |
|
|
78 |
#' |
|
|
79 |
#' @examples |
|
|
80 |
#' l <- c("foo", "bar", "foo", "bars") |
|
|
81 |
#' rename_duplicates(l) |
|
|
82 |
#' |
|
|
83 |
#' @export |
|
|
84 |
rename_duplicates <- function(names) { |
|
|
85 |
tn <- table(names) |
|
|
86 |
for (name in names(tn[tn > 1])) { |
|
|
87 |
names[names == name] <- paste0(name, "_", 1:tn[names(tn) == name]) |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
return(names) |
|
|
91 |
} |