forked from Novartis/tidymodules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.R
68 lines (64 loc) · 2.75 KB
/
snippets.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
59
60
61
62
63
64
65
66
67
68
#'
#' @title Add `{tm}` snippets to RStudio
#'
#' @description This function adds useful `{tm}` code snippets to RStudio.
#'
#' @param force Force the re-installation when the snippets are already installed.
#'
#' @import snippr
#' @import dplyr
#' @importFrom fs path_home_r path_ext_set
#' @importFrom cli cat_bullet
#' @importFrom purrr keep discard map
#' @export
add_tm_snippets <- function(force = FALSE){
# R snippets file
path <- path_home_r(".R", "snippets", path_ext_set("r","snippets"))
if(!create_if_needed(path))
cat_bullet("Skip installation of snippets",
bullet_col = "red",
bullet = "bullet")
# retrieve current and new snippets
current_all_snippets <- snippets_get(path = path)
current_non_tm_snippets <- current_all_snippets %>% discard(grepl("^tm\\.",names(.),perl = TRUE))
current_tm_snippets <- current_all_snippets %>% keep(grepl("^tm\\.",names(.),perl = TRUE))
new_tm_snippets <- snippets_read(path = system.file("rstudio/r.snippets",package = "tidymodules"))
# calculate differences
del_snippets <- setdiff(names(current_tm_snippets),names(new_tm_snippets))
keep_snippets <- intersect(names(current_tm_snippets),names(new_tm_snippets))
add_snippets <- setdiff(names(new_tm_snippets),names(current_tm_snippets))
# print some informations
if(length(del_snippets)>0){
cat_bullet(paste0("Deleting ",length(del_snippets)," snippet(s):"),
bullet_col = "orange",
bullet = "bullet")
invisible(map(del_snippets,cat_bullet,bullet = "dot"))
}
existing_snippets <- current_non_tm_snippets
save_snippets <- NULL
if(length(keep_snippets) > 0)
if(force){
cat_bullet(paste0("Re-installing ",length(keep_snippets)," existing snippet(s):"),
bullet_col = "green",
bullet = "tick")
invisible(map(keep_snippets,cat_bullet,bullet = "dot"))
save_snippets <- new_tm_snippets[keep_snippets]
}else{
cat_bullet(paste0("Skip installation of ",length(keep_snippets)," existing snippet(s):"),
bullet_col = "red",
bullet = "bullet")
invisible(map(keep_snippets,cat_bullet,bullet = "dot"))
existing_snippets <- c(existing_snippets,current_tm_snippets[keep_snippets])
}
if(length(add_snippets)>0){
cat_bullet(paste0("Installing ",length(add_snippets)," new snippets:"),
bullet_col = "green",
bullet = "tick")
invisible(map(add_snippets,cat_bullet,bullet = "dot"))
save_snippets <- c(save_snippets,new_tm_snippets[add_snippets])
}
final_snippets <- existing_snippets
if(!is.null(save_snippets))
final_snippets <- c(final_snippets,save_snippets)
snippets_write(final_snippets,path = path)
}