R/mod_viz_display.R

Defines functions mod_viz_display_server mod_viz_display_ui

#' viz_display UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList
#' @importFrom dplyr %>%
mod_viz_display_ui <- function(id){
  ns <- NS(id)
  tagList(
    uiOutput(ns("viz"))
  )
}
    
#' viz_display Server Function
#'
#' @noRd 
mod_viz_display_server <- function(id, r){
  
  moduleServer(id, function(input, output, session){
    ns <- session$ns
  
  hgch_viz <- reactive({
    req(r$chooseColumns)
    req(r$plot_data)

    # browser()
    data <- r$plot_data
    palette <- r$palette
    
    if(r$colour_method == "colourpalette"){
      palette <- r$palette
    } else if(r$colour_method == "custom"){
      req(r$customColours)
      palette <- r$customColours
    }
    if(is.null(palette)) return()
    opts <- dsvizopts::merge_dsviz_options(color_by = r$fillval, palette_colors = palette,
                                           title = r$title, subtitle = r$subtitle, caption = r$caption,
                                           background_color = r$background_color, dataLabels_type = r$dataLabel_type)
    
    if(!is.null(r$caption)){
      if(nchar(r$caption) > 0){
        opts <- c(opts, plot_margin_bottom = 55)
      }
    }
    
    # browser()
    viz <- do.call(getfun("hgchmagic::hgch_sankey_CatCat"), c(list(data = data, opts = opts
    )))
    
    viz
  })
  
  output$sankeyChart <- highcharter::renderHighchart({
    req(hgch_viz())
    # if(is.null(hgch_viz())) return()
    hgch_viz()
  })
  
  output$viz <- renderUI({
    req(r$dic_draw)
    dic_draw <- r$dic_draw
    if(is.null(dic_draw) | nrow(dic_draw) == 0)return()
    if((!all(dic_draw$class %in% c("hd_Cat", "hd_Dat")) | any(dic_draw$n_distinct > 20)) | length(r$chooseColumns) < 2){
      v <- div(shinypanels::infomessage(type = "warning" , shi18ny::i_("cannot_plot", lang = r$lang(), i18n = r$i18n)),
               shinypanels::infomessage(type = "info" , shi18ny::i_("data_advice", lang = r$lang(), i18n = r$i18n)))
    } else {
      v <- highcharter::highchartOutput(ns("sankeyChart"))
    }
    v
  })
  
  observe({
    r$hgch_viz <- hgch_viz()
  })
  
  })
  
}
 
datasketch/DSAppTemplate documentation built on March 4, 2021, 12:49 a.m.