/
rename_stim.R
44 lines (39 loc) · 1.26 KB
/
rename_stim.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
#' Set stimulus names in a stimlist
#'
#' @param stimuli A list of stimuli
#' @param new_names Vector of new names - must be the same length as the stimlist
#' @param prefix String to prefix to each name
#' @param suffix String to append to each name
#' @param pattern Pattern for gsub
#' @param replacement Replacement for gsub
#' @param ... Additional arguments to pass on to `base::gsub()`
#'
#' @return A list of stimuli with the new names
#' @export
#' @family info
#'
#' @examples
#' demo_stim() |>
#' rename_stim(prefix = "new_") |>
#' names()
rename_stim <- function(stimuli, new_names = NULL, prefix = "", suffix = "",
pattern = NULL, replacement = NULL, ...) {
stimuli <- as_stimlist(stimuli)
if (is.null(new_names)) {
new_names <- names(stimuli)
} else if (length(new_names) != length(stimuli)) {
stop("The length of new_names must be equal to the length of stimuli")
}
# search and replace strings
if (!is.null(pattern) && !is.null(replacement)) {
new_names <- gsub(pattern, replacement,
new_names, ...)
}
# add prefix and/or suffix
new_names <- paste0(prefix, new_names, suffix)
names(stimuli) <- new_names
for (i in seq_along(stimuli)) {
stimuli[[i]]$name <- new_names[i]
}
stimuli
}