Skip to content

Commit

Permalink
Display only compositional statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Dec 15, 2023
1 parent 38ebdcd commit 50ac956
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
4 changes: 4 additions & 0 deletions R/module_coda.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ module_coda_ui <- function(id) {
column(
width = 4,
wellPanel(
helpText("Define the unique identifier (eg. laboratory codes) of each observation."),
selectizeInput(
inputId = ns("codes"),
label = "Unique identifiers",
choices = NULL,
selected = NULL,
multiple = FALSE,
options = list(plugins = "clear_button")
),
helpText("Duplicated sample names will be interpreted as repeated measurements."),
selectizeInput(
Expand All @@ -28,6 +30,7 @@ module_coda_ui <- function(id) {
choices = NULL,
selected = NULL,
multiple = FALSE,
options = list(plugins = "clear_button")
),
helpText("Empty strings can be used to specify that a sample does not belong to any group."),
selectizeInput(
Expand All @@ -36,6 +39,7 @@ module_coda_ui <- function(id) {
choices = NULL,
selected = NULL,
multiple = FALSE,
options = list(plugins = "clear_button")
)
), # wellPanel
## Output: display description
Expand Down
80 changes: 50 additions & 30 deletions R/module_coda_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,42 @@ module_coda_summary_ui <- function(id) {
## Create a namespace function using the provided id
ns <- NS(id)

sidebarLayout(
sidebarPanel(
downloadButton(outputId = ns("download"), "Download")
), # sidebarPanel
mainPanel(
fluidRow(
column(
width = 4,
wellPanel(
style = "margin-bottom: 15px;",
h5("Compositional statistics"),
downloadButton(outputId = ns("download"), "Download tables")
), # wellPanel
output_plot(id = ns("dendrogram"), height = "auto", title = "Variation matrix")
), # column
column(
width = 8,
tabsetPanel(
type = "tabs",
tabPanel(
title = "Summary statistics",
h5("Center"),
tableOutput(outputId = ns("location"))
tableOutput(outputId = ns("location")),
h5("Percentile table"),
tableOutput(outputId = ns("quantile"))
),
tabPanel(
title = "Univariate statistics",
output_plot(id = ns("hist"), height = "auto", title = "Histogram")
),
tabPanel(
title = "CLR covariance",
tableOutput(outputId = ns("covariance"))
),
tabPanel(
title = "Variation matrix",
fluidRow(
div(
class = "col-lg-6 col-md-1",
output_plot(id = ns("dendrogram"), height = "auto", title = "Heatmap")
),
div(
class = "col-lg-6 col-md-1"
)
),
fluidRow(
tableOutput(outputId = ns("variation"))
)
tableOutput(outputId = ns("variation"))
)
) # tabsetPanel
) # mainPanel
) # sidebarLayout
) # column
) # fluidRow
}

# Server =======================================================================
Expand All @@ -60,55 +62,73 @@ module_coda_summary_server <- function(id, x) {
stopifnot(is.reactive(x))

moduleServer(id, function(input, output, session) {
## Location
## Location -----
data_loc <- reactive({
req(x())
if (nexus::any_assigned(x())) {
nexus::aggregate(x(), by = "groups", FUN = mean, na.rm = FALSE)
index <- nexus::get_groups(x())
nexus::aggregate(x(), by = index, FUN = nexus::mean, na.rm = FALSE)
} else {
moy <- colMeans(x(), na.rm = FALSE)
matrix(moy, ncol = length(moy), dimnames = list("", names(moy)))
m <- nexus::mean(x(), na.rm = FALSE)
matrix(m, nrow = 1, dimnames = list("center", names(m)))
}
})

## CLR covariance
## Percentiles -----
data_quant <- reactive({
req(x())
nexus::quantile(x(), probs = seq(0, 1, 0.25))
})

## Histograms -----
plot_hist <- reactive({
req(x())
nexus::hist(x()[, 1:6, drop = FALSE], ncol = 3)
grDevices::recordPlot()
})

## CLR covariance -----
data_cov <- reactive({
req(x())
nexus::covariance(x(), center = TRUE)
})

## Variation matrix
## Variation matrix -----
data_var <- reactive({
req(x())
nexus::variation(x())
})

## Clustering
## Clustering -----
# plot_clust <- reactive({
# d <- stats::as.dist(data_var())
# h <- stats::hclust(d, method = "ward.D2")
# plot(h, main = "", sub = "", xlab = "", ylab = "Total variation", las = 1)
# grDevices::recordPlot()
# })

## Heatmap -----
plot_heatmap <- reactive({
stats::heatmap(data_var(), distfun = stats::as.dist,
hclustfun = function(x) stats::hclust(x, method = "ward.D2"),
symm = TRUE, scale = "none")
grDevices::recordPlot()
})

## Render table
## Render table -----
output$location <- renderTable({data_loc()}, striped = TRUE, width = "100%", rownames = TRUE)
output$quantile <- renderTable({data_quant()}, striped = TRUE, width = "100%", rownames = TRUE)
output$covariance <- renderTable({data_cov()}, striped = TRUE, width = "100%", rownames = TRUE)
output$variation <- renderTable({data_var()}, striped = TRUE, width = "100%", rownames = TRUE)

## Render plot
## Render plot -----
render_plot("dendrogram", x = plot_heatmap)
render_plot("hist", x = plot_hist, height = function() { getCurrentOutputInfo(session)$width() / 2 })

## Download
## Download -----
output$download <- export_table(
location = data_loc,
quantiles = data_quant,
covariance = data_cov,
variation = data_var,
name = "coda_summary"
Expand Down
1 change: 0 additions & 1 deletion inst/coda/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ shiny_server <- function(input, output, session) {
coda <- janus::module_coda_server("coda", x = clean)

## Statistics
janus::module_summary_server("summary", coda)
janus::module_coda_summary_server("coda_summary", coda)

## Graphs
Expand Down
11 changes: 2 additions & 9 deletions inst/coda/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,9 @@ shiny_ui <- function(request) {
) # tabPanel
) # tabsetPanel
), # tabPanel
navbarMenu(
tabPanel(
title = "Statistics",
tabPanel(
title = "Classical statistics",
janus::module_summary_ui("summary")
), # tabPanel
tabPanel(
title = "Compositional statistics",
janus::module_coda_summary_ui("coda_summary")
) # tabPanel
janus::module_coda_summary_ui("coda_summary")
), # tabPanel
navbarMenu(
title = "Graph",
Expand Down

0 comments on commit 50ac956

Please sign in to comment.