-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodule_rds.R
101 lines (89 loc) · 2.99 KB
/
module_rds.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
#' rds tab UI
#' Module to create a tabset panel to allow the download of the rds file
#' @param id ID for shiny module namespacing
#' @noRd
rdsUI = function(id) {
ns = shiny::NS(id)
# RDS files tab panel
downloader_tab_panel(title = "RDS Files",
chooser_id = ns("rds_type"),
download_button_id = ns("download_rds"),
panel = display_panel(shiny::uiOutput(ns("display_rds"))))
}
#' rds tab Server
#' @param id ID for shiny module namespacing
#' @param cluster_choice which cluster to display the data for
#' @param data_dir The data directory for the app.
#' @noRd
rdsServer = function(id, cluster_choice, data_dir) {
shiny::moduleServer(id, function(input, output, session) {
ns = session$ns # nolint
# disable dropdown initially
shiny::observe({
shinyjs::disable("rds_type")
})
# all available rds
all_files = shiny::reactive({
return(get_all_files(cluster_choice(), data_dir = data_dir))
}) %>%
shiny::bindCache(cluster_choice())
# drop down for rds
shiny::observeEvent(all_files(), {
all_rds = filter_by_filetype(filenames = all_files(),
filetypes = c("rds", "RDS"))
if (length(all_rds) != 0) {
shinyjs::enable("rds_type")
} else {
shinyjs::disable("rds_type")
}
shiny::updateSelectInput(session,
"rds_type",
label = "Select RDS file:",
choices = all_rds)
})
# get rds file
rds_file = shiny::reactive({
shiny::req(cluster_choice())
rds_file = file.path(data_dir, "scanner_output", cluster_choice(), input$rds_type)
return(rds_file)
}) %>%
shiny::bindCache(cluster_choice(), input$rds_type)
# check if plots available
rds_avail = shiny::reactive({
shiny::req(rds_file())
src = fs::path_rel(
rds_file(),
file.path(data_dir, "scanner_output")
)
if (length(src) != 0) {
return(grepl(".rds", tolower(src)))
} else {
return(FALSE)
}
})
# display message if rds available
output$display_rds = shiny::renderUI({
if (rds_avail()) {
shiny::p("Click below to download the file.", style = "color: black; text-align: left")
} else {
shiny::p("No RDS files available.", style = "color: red; text-align: left")
}
})
# disable download button if no rds files available / none selected
shiny::observeEvent(rds_avail(), {
shinyjs::toggleState("download_rds", condition = rds_avail())
})
shiny::observeEvent(input$rds_type, {
shinyjs::toggleState("download_rds", condition = input$rds_type != "")
})
# download plot
output$download_rds = shiny::downloadHandler(
filename = function() {
glue::glue("{cluster_choice()}_{input$rds_type}")
},
content = function(file) {
file.copy(rds_file(), file)
}
)
})
}