R/mod_indel_distribution.R

Defines functions mod_indel_distribution_server mod_indel_distribution_ui

#' indel_distribution UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_indel_distribution_ui <- function(id){
  ns <- NS(id)

  tagList(column(
    width = 12,
    shiny::div(
      class = "row g-3 justify-content-start",
      shiny::div(class = "d-inline p-2",
                 mod_sample_meta_filters_ui(ns("indel_filters"))),
      shiny::div(
        class = "d-inline p-2",
        shinyWidgets::dropMenu(
          arrow = FALSE,
          tag = shiny::actionButton(
            inputId = ns("configure"),
            label = "Configure",
            icon = shiny::icon("cogs"),
            class = "btn-primary"
          ),
        tagList(
          shiny::div(class = "compact-dropdown",
            custom_picker_menu(
              .id = ns("color"),
              .label = "Color by",
              .choices = c("none"),
              selected = "none",
              multiple = FALSE,
              width = "100%"
            )
          ),
          shiny::div(class = "compact-dropdown",
            custom_picker_menu(
              .id = ns("facet"),
              .label = "Facet by",
              .choices = c("none"),
              selected = "none",
              multiple = FALSE,
              width = "100%"
            )
          ),
          shiny::div(class = "compact-dropdown",
            shiny::sliderInput(
              inputId = ns("trunc_slider"),
              label = "Truncate indel species range",
              min = -50,
              max = 50,
              value = c(-15, 15),
              step = 1
            )
          ),
          shiny::div(class = "compact-dropdown",
            shiny::numericInput(
              inputId = ns("plot_width"),
              label = "Chart width",
              value = 800,
              min = 300,
              max = 2000,
              step = 10
            )
          ),
          shiny::div(class = "compact-dropdown",
            shiny::numericInput(
              inputId = ns("plot_height"),
              label = "Chart height",
              value = 500,
              min = 300,
              max = 2000,
              step = 10
            )
          )
        )
      )
    ),
      shiny::div(class = "d-inline p-2",
                 shinyWidgets::dropMenu(
                   arrow = FALSE,
                   maxWidth = "300px",
                   tag = shiny::actionButton(
                     inputId = ns("help"),
                     label = "Help",
                     icon = shiny::icon("info-circle"),
                     class = "btn-primary"
                   ),
                   shiny::includeMarkdown("inst/md/indel_dist_help.md")
                 )
      )
    ),
    shiny::hr(),
    plotly::plotlyOutput(outputId = ns("indel_distribution"))
  ))

}

#' indel_distribution Server Functions
#'
#' @noRd
mod_indel_distribution_server <- function(id, app_data){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    filters_out <-
      mod_sample_meta_filters_server(id = "indel_filters",
                                     app_data = app_data,
                                     target_table = "AppIndels")
    RV <- shiny::reactive({
      filters_out$get()
    })

    shiny::observe({
      dropdown_options <- find_possible_meta_columns(RV()$data_to_render)

      shinyWidgets::updatePickerInput(
        session = session,
        inputId = "color",
        label = "Color by",
        choices = c("none", dropdown_options),
        selected = "none"
      )

      shinyWidgets::updatePickerInput(
        session = session,
        inputId = "facet",
        label = "Facet by",
        choices = c("none", dropdown_options),
        selected = "none"
      )
    }) %>% shiny::bindEvent({ RV()$data_to_render })


    output$indel_distribution <- plotly::renderPlotly({
      # shiny::req(input$color)
      # shiny::req(input$facet)

      shiny::validate(shiny::need(
        shiny::isTruthy(app_data$project),
        message = "Please select a project in the `Project selection' tab."))

      shiny::validate(
        shiny::need(
          nrow(RV()$data_to_render) > 0,
          message = "Found no data matching your query. Please adjust your filters.")
      )

      RV()$data_to_render %>%
        indel_species_plot(
          .gvar = input$color,
          .fvar = input$facet,
          .cut_points = input$trunc_slider,
          .dims = c(input$plot_width, input$plot_height)
        )
    })

    shiny::onRestored(function(state) {
      shiny::withProgress(message = "Restoring session. Please wait",
                          expr = {
                            dropdown_options <- find_possible_meta_columns(RV()$data_to_render)

                            shinyWidgets::updatePickerInput(
                              session = session,
                              inputId = "color",
                              label = "Color by",
                              choices = c("none", dropdown_options),
                              selected = state$input$color
                            )

                            shinyWidgets::updatePickerInput(
                              session = session,
                              inputId = "facet",
                              label = "Facet by",
                              choices = c("none", dropdown_options),
                              selected = state$input$facet
                            )
                          })
    })

  })
}
teofiln/gene.editing.dash documentation built on Feb. 21, 2022, 12:59 a.m.