-
Notifications
You must be signed in to change notification settings - Fork 0
/
contrastPlot.R
173 lines (167 loc) · 6.29 KB
/
contrastPlot.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#' Shiny Module Server for Contrast Plots
#'
#' @param id identifier
#' @param panel_par,main_par input parameters
#' @param contrast_table reactive data frame
#' @param customSettings list of custom settings
#' @param modTitle character string title for section
#'
#' @return reactive object
#' @importFrom shiny column fluidRow moduleServer NS observeEvent
#' radioButtons reactive reactiveVal reactiveValues renderUI
#' req selectInput tagList uiOutput updateSelectInput
#' @importFrom DT renderDataTable
#' @importFrom foundr ggplot_conditionContrasts summary_conditionContrasts
#' summary_strainstats
#' @export
#'
contrastPlotServer <- function(id, panel_par, main_par,
contrast_table, customSettings = NULL,
modTitle = shiny::reactive("Contrasts")) {
shiny::moduleServer(id, function(input, output, session) {
ns <- session$ns
plot_par <- plotParServer("plot_par", contrast_table)
volcano <- volcanoServer("volcano", panel_par, plot_par, plot_info,
contrast_table)
biplot <- biplotServer("biplot", panel_par, plot_par, plot_info,
contrast_table)
dotplot <- dotplotServer("dotplot", panel_par, plot_par, plot_info,
contrast_table)
# Plot Info
has_contrasts <- shiny::reactive({
inherits(shiny::req(contrast_table()), "conditionContrasts")
})
plot_row = shiny::reactive({
ifelse(has_contrasts(), "strain", "term")
})
plot_info <- shiny::reactiveValues(
# Threshold List.
threshold = shiny::reactive({
shiny::req(plot_par$volvert, plot_par$volsd, plot_par$ordername)
out <- c(SD = plot_par$volsd, p.value = 0.01, kME = 0.8, module = 10,
size = 15)
out[plot_par$ordername] <- ifelse(plot_par$ordername == "p.value",
10 ^ -plot_par$volvert, plot_par$volvert)
out
}),
# Filter to desired rownames (strains or terms).
rownames = shiny::reactive({
shiny::req(contrast_table(), plot_par$rownames, plot_row())
dplyr::filter(contrast_table(),
.data[[plot_row()]] %in% plot_par$rownames)
})
)
output$plot_table <- shiny::renderUI({
shiny::tagList(
shiny::h3(modTitle()),
switch(shiny::req(main_par$plot_table),
Plots = shiny::tagList(
shiny::uiOutput(ns("plot_choice")),
shiny::uiOutput(ns("plot"))),
Tables = DT::renderDataTable(tableObject(), escape = FALSE,
options = list(scrollX = TRUE, pageLength = 10))))
})
output$plot_choice <- shiny::renderUI({
choices <- c("Volcano","BiPlot","DotPlot")
shiny::checkboxGroupInput(ns("plot_choice"), "",
choices = choices, selected = choices, inline = TRUE)
})
output$plot <- shiny::renderUI({
shiny::req(input$plot_choice)
shiny::tagList(
if("Volcano" %in% input$plot_choice) volcanoOutput(ns("volcano")),
if("BiPlot" %in% input$plot_choice) biplotOutput(ns("biplot")),
if("DotPlot" %in% input$plot_choice) dotplotOutput(ns("dotplot")))
})
tableObject <- shiny::reactive({
shiny::req(contrast_table())
title <- ifelse(has_contrasts(), "Strains", "Terms")
if(title == "Strains") {
foundr::summary_conditionContrasts(
dplyr::filter(contrast_table(), sex == shiny::req(panel_par$sex)),
ntrait = 0)
} else { # title == "Terms"
foundr::summary_strainstats(contrast_table(),
stats = "log10.p", model = "terms",
threshold = c(p.value = 1.0, SD = 0.0))
}
})
output$vol_sliders <- shiny::renderUI({
if(shiny::req(main_par$plot_table) == "Plots")
plotParUI(ns("plot_par")) # volsd, volvert (sliders)
})
###############################################################
shiny::reactiveValues(
panel = shiny::reactive(NULL),
postfix = shiny::reactive({
shiny::req(contrast_table())
paste(unique(contrast_table()$dataset), collapse = ",")
}),
plotObject = shiny::reactive({
if("Volcano" %in% input$plot_choice)
print(shiny::req(volcano()))
if("BiPlot" %in% input$plot_choice)
print(shiny::req(biplot()))
if("DotPlot" %in% input$plot_choice)
print(shiny::req(dotplot()))
}),
tableObject = tableObject)
})
}
#' Shiny Module UI for Contrast Plots
#' @return nothing returned
#' @rdname contrastPlotServer
#' @export
contrastPlotUI <- function(id) {
ns <- shiny::NS(id)
plotParInput(ns("plot_par")) # ordername, interact
}
#' Shiny Module Output for Contrast Plots
#' @return nothing returned
#' @rdname contrastPlotServer
#' @export
contrastPlotOutput <- function(id) {
ns <- shiny::NS(id)
shiny::tagList(
shiny::uiOutput(ns("vol_sliders")), # volsd, volvert (sliders)
plotParOutput(ns("plot_par")), # rownames (strains/terms)
shiny::uiOutput(ns("plot_table")))
}
#' Shiny App for Contrast Plots
#'
#' @return nothing returned
#' @rdname contrastPlotServer
#' @export
contrastPlotApp <- function() {
title <- "Test contrastPlot Module"
ui <- function() {
shiny::fluidPage(
shiny::titlePanel(title),
shiny::sidebarLayout(
shiny::sidebarPanel(
mainParInput("main_par"), # dataset
mainParUI("main_par"), # order
border_line(),
mainParOutput("main_par"), # plot_table, height
downloadOutput("download")
),
shiny::mainPanel(
shiny::fluidRow(
shiny::column(4, panelParUI("panel_par")), # sex
shiny::column(8, contrastPlotUI("contrast_plot"))), # ordername, interact
contrastPlotOutput("contrast_plot") # volsd, volvert, rownames
)
)
)
}
server <- function(input, output, session) {
main_par <- mainParServer("main_par", traitStats)
panel_par <- panelParServer("panel_par", main_par, traitStats)
contrast_table <- contrastTableServer("contrast_table", main_par,
traitSignal, traitStats, customSettings)
contrast_list <- contrastPlotServer("contrast_plot", panel_par, main_par,
contrast_table, customSettings)
downloadServer("download", "Contrasts", main_par, contrast_list)
}
shiny::shinyApp(ui = ui, server = server)
}