-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.r
138 lines (116 loc) · 3.38 KB
/
utils.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
library(DT)
library(readxl)
random_palette <- function(n){
sample(grDevices::colors()[grep("gr(a|e)y", grDevices::colors(), invert = T)], n)
}
## update 04/07/22 add NA test
## update 10/08/2022 add sort
concat_uniq <- function(x, sep = ","){
res <- paste0(sort(unique(na.omit(x))), collapse = sep)
if(res == "") {res <- NA}
res
}
concat <- function(x, sep = ","){
res <- paste0(na.omit(x), collapse = sep)
if(res == "") {res <- NA}
res
}
concat_uniq2 <- function(..., sep = ","){
vec <- paste(na.omit(c(...)), collapse = sep)
res <- gsub("^,", "", paste(unique(sort(stringr::str_split(vec, pattern = ",")[[1]])), collapse = sep))
res
}
rm_brackets <- function(x){
str_replace_all(x, "\\[|\\]|\\ ", "")
}
rm_special_char <- function(x){
str_replace_all(x, "\\[|\\]|\\ |\\|", "")
}
capitalize <- function(x){
CapStr <- function(y) {
c <- strsplit(y, " ")[[1]]
paste(toupper(substring(c, 1,1)), substring(c, 2),
sep="", collapse=" ")
}
sapply(x, CapStr)
}
## label plots
label_box <- function(x){return(c(y = median(x), label = length(x)))}
### upset get binary table with rownames
fromList <- function (input) {
# Same as original fromList()...
elements <- unique(unlist(input))
data <- unlist(lapply(input, function(x) {
x <- as.vector(match(elements, x))
}))
data[is.na(data)] <- as.integer(0)
data[data != 0] <- as.integer(1)
data <- data.frame(matrix(data, ncol = length(input), byrow = F))
data <- data[which(rowSums(data) != 0), ]
names(data) <- names(input)
# ... Except now it conserves your original value names!
elements[is.na(elements)] <- "NA" #### added 2022-09-27
row.names(data) <- elements
return(data)
}
## wrapper function to remove commented rows from excel sheets
read_excel_cm <- function(file, sheet, comment.char = "#", na = c("", "NA")){
tab <- read_excel(file, sheet, col_names = F, na = na)
skip_rows <- grep(paste0("^", comment.char), unlist(c(tab[,1])))
if(length(skip_rows) > 0) tab <- read_excel(file, sheet, skip = max(skip_rows))
return(tab)
}
#--------------- plots
#---- x
simple_hist <- function(df, x){
ggplot(df, aes(get(x))) +
geom_histogram(stat = "count") +
labs(
title = deparse(substitute(df)),
subtitle = "",
caption = ".",
tag = "",
x = x,
# colour = "Legend title"
) +
theme_minimal()
}
# simple_hist(iris, "Petal.Width")
#---- xy
simple_plot <- function(df, x, y){
ggplot(df, aes(get(x), get(y))) +
geom_point()
# ggtitle("") +
# labs(x = "", y = "")
}
# simple_plot(iris, "Sepal.Length", "Petal.Width")
simple_line <- function(df, x, y){
ggplot(df, aes(get(x), get(y))) +
geom_line()
# ggtitle("") +
# labs(x = "", y = "")
}
# simple_line(iris, "Sepal.Length", "Petal.Width")
simple_bar <- function(df, x, y){
ggplot(df, aes(get(x), get(y))) +
geom_bar(stat = "identity") +
ggtitle(deparse(substitute(df))) +
labs(x = deparse(substitute(x)), y = deparse(substitute(y)))
}
# simple_bar(iris, "Species", "Sepal.Length") + coord_flip()
## add inheritance for further params custom - wrapper function
simple_box <- function(df, x, y) {
ggplot(df, aes(get(x), get(y))) +
geom_boxplot() +
labs(
title = deparse(substitute(df)),
subtitle = "",
caption = ".",
tag = "",
x = x,
y = y,
# colour = "Legend title"
) +
theme_minimal()
}
# simple_box(iris, "Species", "Petal.Width")