-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote_mutate.R
71 lines (52 loc) · 2.26 KB
/
quote_mutate.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
#name: samuel ahuno
#how do quotign work in R base, tidyverse and dplyr
#tidyverse - use `{{var_name}}` while within the function to get col names
#data.table - use `get(var_name)` while within the function to get col names
library(tidyverse)
library(dplyr)
#### generate random numbers
random_numb_generator = function(x, numb=25){
treatment_x1 = runif(n=numb, min=-5, max=5)
treatment_x2 = runif(n=numb, min=0, max=5)
return(cbind(treatment_x1,treatment_x2))
}
n_unique_samples <- 5
ls <- lapply(1:n_unique_samples, random_numb_generator, numb=25)
# convert list to dataframe
df <- do.call(data.frame, ls)
##rename columns of data frame
names_raw <- c(paste0("treatment_", letters[1:n_unique_samples],1:2),
paste0("treatment_", letters[1:n_unique_samples],c(2,1)))
names(df) <- sort(names_raw)
#add tag
df <- df %>% mutate(gene.symbol = paste0("gene", row_number()))
plot_exp <- function(dat_in, y_var, labels_col){
y_var <- enquo(y_var)
y_var_name = quo_name(y_var)
#enquo variables
# labels_col <- enquo(labels_col)
# #y_var_sym <- ensym(y_var)
# y_var <- enquo(y_var)
# ggsave_tag <- quo_name(y_var)
#multiple_vars <- enquos(...)
# print(paste0("enquo - y_var -> ",y_var))
# print(paste0("quo_name - y_var -> ",ggsave_tag))
# print("{{labels_col}}")
#mutate df to allow plotting
dat_log2 <- dat_in %>% mutate(labels = case_when({{labels_col}} >= abs(2) ~ as.character(gene.symbol),
TRUE ~ ""),
color_codes = case_when(labels == "" ~ "Log2FC_below2",
TRUE ~ "Log2FC_above2")) %>%
select(c({{y_var}}, {{labels_col}}, labels, color_codes, gene.symbol))
#head(dat_log2)
print(paste0("/path/to/save/",{{y_var_name}},".pdf"))
return(dat_log2)
}
#test function
plot_exp(dat_in=df, y_var=treatment_a2, labels_col=treatment_a1)
#create separate vectors
col1 <- sort(names_raw)[str_detect(sort(names_raw),"1")]
col2 <- sort(names_raw)[str_detect(sort(names_raw),"2")]
map2(col1,col2, ~paste0(.x, .y))
map2(col1,col2, ~plot_exp(dat_in=df, y_var=!!sym(.x), labels_col=!!sym(.y))) #this works
#map2(col1,col2, ~plot_exp(dat_in=df, y_var=all_of(.x), labels_col=all_of(.y))) # this doesnot work