-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_server.R
133 lines (116 loc) · 4.83 KB
/
data_server.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
data_server <- function(input, output, session) {
output$fileUploaded <- reactive({ return(!is.null(input$files)) })
outputOptions(output, 'fileUploaded', suspendWhenHidden = FALSE)
output$fileExcel <- reactive({
if (!is.null(input$files) &&
!is.na(excel_format(input$files$datapath))) {
updateSelectInput(session, "sheet", choices = excel_sheets(input$files$datapath))
# updateSelectInput(session, "clean_col_1", choices = c("None", colnames(get_pheno())))
return(TRUE)
}
updateSelectInput(session, "sheet", choices = character(0))
return(FALSE)
})
outputOptions(output, 'fileExcel', suspendWhenHidden = FALSE)
vals <- reactiveValues(data = NULL)
observe({ req(input$files)
output$ui_clean_cols <- renderUI({ req(input$files)
tagList(
varSelectInput("clean_cols", "Column(s) to Number", multiple = TRUE, data = vals$data),
p("Select Columns to remove all non-digits and convert to number."),
varSelectInput("na_cols", "NA to Missing", multiple = TRUE, data = vals$data),
p('Select Columns to convert "NA" to "Missing"'))
})
})
# Move this to a utilities file?
import_file <- function(file, col_names, delim, quote, skip) {
if (is.na(excel_format(file))) {
file <- read_delim(file, col_names = col_names,
delim = delim, quote = quote, skip = skip)
} else {
file <- read_excel(file, skip = skip, col_names = col_names)
}
file
}
# Have to put these in global environment for now. Rewrite using moduleServer and nested servers.
read_pheno <- reactive({ req(input$files)
pheno <- import_file(input$files$datapath, input$col_names, input$delim, input$quote, input$skip)
updateSelectInput(session, "d_id_col", choices = colnames(pheno))
updateSelectInput(session, "m_id_col_2", choices = colnames(pheno))
map(list("align_cols", "filter_cols", "by_cols"),
~ updateCheckboxGroupInput(session, .x, choices = set_names(colnames(pheno))))
vals$data <- pheno
})
# Have to put these in global environment for now. Rewrite using moduleServer and nested servers.
get_pheno <- reactive({ req(input$files)
# pheno <- read_pheno()
pheno <- read_pheno()
req(input$d_id_col %in% colnames(pheno))
pheno %>%
possibly(rename, otherwise = ., )("Sample ID_old" = "Sample ID") %>%
rename("Sample ID" = input$d_id_col) %>%
mutate(`Sample ID` = as.character(`Sample ID`))
})
clean_pheno <- reactive({
make_numeric <- function(x) { str_remove_all(x, "[^[:digit:].]") %>% as.double() }
na_to_missing <- function(x) { ifelse(is.na(x), "Missing", x) }
get_pheno() %>%
when(length(input$clean_cols) < 1 ~ .,
mutate(., across(as.character(input$clean_cols), make_numeric))) %>%
when(length(input$na_cols) < 1 ~ .,
mutate(., across(as.character(input$na_cols), na_to_missing)))
})
observeEvent(input$getFilterColumns, {
# filter_choices <- colnames(clean_pheno())
showModal(modalDialog(
checkboxGroupInput("filter_cols", "Select Columns for Filtering", choices = colnames(clean_pheno())),
actionButton("filterSelectedColumns", "Filter Selected"),
easyClose = TRUE,
footer = NULL
))
})
filter_names <- reactive({ req(input$filter_cols)
input$filter_cols %>%
map(~ str_c("filt_", make.names(.))) %>%
set_names(input$filter_cols, .)
})
has_filter <- reactive({ req(input$filter_cols)
filter_names() %>% names() %>%
map( ~ pluck(input, .) %>% length() > 0) %>%
reduce(all)
})
observeEvent(input$filterSelectedColumns , {
output$ui_filter_cols <- renderUI({
tagList(
filter_names() %>%
imap(~ checkboxGroupInput( inputId = .y, label = .x,
choices = pull(clean_pheno(), .x) %>% unique(),
selected = pull(clean_pheno(), .x) %>% unique())),
)
})
removeModal()
})
filter_pheno <<- reactive({ req(filter_names)
if (! has_filter()) { clean_pheno() }
else {
data_filters <- filter_names() %>%
imap(~ str_c(.x, " %in% c(", str_c('"', pluck(input, .y), '"', collapse = ', '), ")")) %>%
unlist()
clean_pheno() %>%
filter_(data_filters)
}
})
createTableOutput <- function(df) {
if (input$d_disp == "head") { return(head(df)) }
return(df)
}
output$pheno <- renderTable({ createTableOutput(get_pheno()) })
output$cleaned_pheno <- renderTable({ createTableOutput(clean_pheno()) })
output$filtered_pheno <- renderTable({ createTableOutput(filter_pheno()) })
output$downloadCleanData <- downloadHandler(
filename = function() { str_c('cleaned_and_filtered_data-', Sys.Date(), '.tsv') },
content = function(con) { write_tsv(filter_pheno(), file = con) })
output$debug <- renderText({ # req(input$fileUploaded)
input$d_id_col
})
}