#' Saldae Dashboard Module Anomaly Detection Advanced
#' @description t.b.d
#' @author Farid Azouaou
#' @param id t.b.d
#' @export
SA_anomaly_UI <- function(id,mod_title = NULL ,div_width = "col-xs-12 col-sm-12 col-md-12") {
ns <- NS(id)
bs4Dash::tabBox(width = 12, title = mod_title,status = "info", solidHeader = TRUE,
tabPanel(icon("table"),
uiOutput(ns("anomaly_board_box")),
DT::dataTableOutput(ns("tisefka_table"))
),
tabPanel(icon("fas fa-chart-bar"),
fluidPage(
uiOutput(ns("variable_anomaly_view")),
SaldaeReporting::add_to_report_ui(id = ns("add_anomaly")),
dygraphs::dygraphOutput(ns("tisefka_plot"),height = "700px")
)
)
)
}
#' Saldae Dashboard Module Server
#' @description Saldae Dashboard module SERVER : render and generate object to be displayed data(chart/table)
#' @author Farid Azouaou
#' @param input input shinydashboard elements containing information to use for output generation
#' @param output output shinydashboard element
#' @param session shiny session
#' @param tisefka reactive object containing data
#' @return output objects to be displayed in corresponding UI module
#' @export
SA_anomaly_mod <- function(input, output, session, i18n, tisefka) {
tisefka_choices <- reactive({
req(tisefka())
tisefka()$numeric_variables
})
tisefka_tizegzawin <- reactive({
req(tisefka())
tisefka()$tisefka_tizegzawin
})
non_numeric_variables <- reactive({
req(tisefka())
tisefka()$non_numeric_variables
})
categoricals_unique_values <- reactive({
req(tisefka())
tisefka()$categoricals_unique_values
})
ts_time_units <- reactive({
tisefka()$ts_time_units
})
ns <- session$ns
output$anomaly_board_box <- renderUI({
bs4Dash::box(title = i18n$t("Anomaly Board"),collapsible = TRUE,
status = "info",width = 12,solidHeader = TRUE,
#-----HEADER CONTENT
fluidRow(
column(width = 3,uiOutput(ns("select_element"))) ,
column(width = 2,uiOutput(ns("var_granularity"))),
column(width = 2,uiOutput(ns("aggregation_metric"))),
column(width = 2,shiny::br(), uiOutput(ns("submit")))
),
uiOutput(ns("non_numeric_variables_inputs"))
)
})
output$submit <- renderUI({
req(input$variable_picker)
bs4Dash::actionButton(inputId = ns("submit"), label = i18n$t("Start"), icon = icon("play"), status = "info")
})
observeEvent(eventExpr=non_numeric_variables(),handlerExpr= {
non_numeric_variables()%>%purrr::imap( ~{
output_name_app <- paste0("non_numeric_variables_", .x)
output[[output_name_app]] <- renderUI({
ml_choices <- tisefka()$var_factors[[.x]]
shinyWidgets::pickerInput(
inputId = ns(output_name_app),
label = gsub("_"," ",.x),
choices = categoricals_unique_values()[[.x]],
options = list(
`actions-box` = TRUE,
size = 10,
`selected-text-format` = "count > 3"
),
multiple = TRUE
)
})
})
})
output$non_numeric_variables_inputs <- renderUI({
req(non_numeric_variables())
fluidRow(
purrr::map(non_numeric_variables(), ~{
column(width = 2, uiOutput(ns(paste0("non_numeric_variables_",.x))))
})
)
})
output$select_element <- renderUI({
req(tisefka_tizegzawin())
shinyWidgets::pickerInput(inputId = ns("variable_picker"),
label = i18n$t("Target variables"),
multiple = TRUE,
choices = tisefka_choices(),
selected = NULL)
})
output$var_granularity <- renderUI({
req(non_numeric_variables())
shinyWidgets::pickerInput(inputId = ns("var_granularity"),
label = i18n$t("Granularity"),
multiple = TRUE,
choices = non_numeric_variables(),
selected = NULL
)
})
# aggregation metric
output$aggregation_metric <- renderUI({
req(non_numeric_variables())
aggregation_choices <- c("Average","Sum","Min","Max","Median")
names(aggregation_choices) <- i18n$t(aggregation_choices)
shinyWidgets::pickerInput(inputId = ns("aggregation_metric"),
label = i18n$t("Aggregation"),
multiple = FALSE,
selected = aggregation_choices[1],
choices = aggregation_choices
)
})
tisefka_iheggan <- reactive({
req(tisefka_tizegzawin())
req(input$variable_picker)
aggreg_fun <- SA_aggregation_funs(aggregation_metric = input$aggregation_metric )
tisefka_iheggan <- tisefka_tizegzawin()
if(length(non_numeric_variables())>0){
categ_input_filter <-non_numeric_variables()%>%purrr::map(~input[[paste0("non_numeric_variables_",.x)]])%>%
stats::setNames(non_numeric_variables())
categ_input_filter <- categ_input_filter[!unlist(lapply(categ_input_filter, is.null))]
for(cat_input in names(categ_input_filter)){
tisefka_iheggan <- tisefka_iheggan%>%dplyr::filter(!!rlang::sym(cat_input)%in%categ_input_filter[[cat_input]])
}
}
if(is.null(input$var_granularity)){
if(is.null(aggreg_fun)) aggreg_fun <- sum
tisefka_iheggan<- tisefka_iheggan%>%dplyr::select(date,!!input$variable_picker)%>%
dplyr::group_by(date)%>%dplyr::summarise_all(aggreg_fun)
}else{
list_val_fn <- input$variable_picker%>%purrr::map(~aggreg_fun)%>%stats::setNames(input$variable_picker)
tisefka_iheggan<- tisefka_iheggan %>%
tidyr::pivot_wider(
id_cols = date,
names_from = input$var_granularity,
values_from = input$variable_picker,
values_fn = list_val_fn)
}
tisefka_iheggan <- tisefka_iheggan%>%dplyr::arrange(date)%>%
dplyr::group_by(date)%>%dplyr::summarise_all(aggreg_fun,na.rm = TRUE)
max_variables <- min(ncol(tisefka_iheggan),15)
tisefka_iheggan <- tisefka_iheggan[1:max_variables]
return(tisefka_iheggan)
})
target_variables <- reactive({
req(tisefka_iheggan())
target_variables <- colnames(tisefka_iheggan())
target_variables <- target_variables[target_variables!="date"]
return(target_variables)
})
tisefka_anomaly <- eventReactive(input$submit,{
req(tisefka_iheggan())
req(target_variables())
SaldaeDataExplorer::anomaly_detection_nnegh(tisefka_iheggan(),target_ts = target_variables(),anomaly_mode = "anomalize")
})
output$variable_anomaly_view <- renderUI({
req(target_variables())
shinyWidgets::pickerInput(inputId = ns("variable_anomaly_view"),
label = i18n$t("Select Variable"),
multiple = FALSE,
choices = target_variables(),
selected = target_variables()[1]
)
# %>%shinyhelper::helper(type = "markdown",buttonLabel="Got it",
# icon= shiny::icon("fas fa-question-circle"),
# colour = "orange",
# fade = FALSE,
# size = "l",
# content = "sald_anomaly")
})
#----------------main chart
output$tisefka_table <- DT::renderDataTable({
req(tisefka_anomaly())
SaldaeDataExplorer::anomaly_to_DT_insight(tisefka_anomaly())
})
tisefka_plot <- reactive({
req(input$variable_anomaly_view)
tisefka_anomaly()[[input$variable_anomaly_view]]%>%
SaldaeDataExplorer::SA_anomaly_charter(target_variable = input$variable_anomaly_view)
})
output$tisefka_plot <- dygraphs::renderDygraph({
tisefka_plot()
})
# add to report
report_dir <- "./thaink2_report/"
item_elements <- reactive({
req(tisefka_anomaly())
req(tisefka_plot())
data_result <- list(output_data = tisefka_anomaly()[[input$variable_anomaly_view]],
output_graph = tisefka_plot(),
output_comment = "output_comment")
graph_type <- "lines"; categoricals <- NULL; output_type <- c("anomaly_detection");time_frequency <- "hours"
list(
data_result = data_result, # result to include into the report, can be table or graph or both
granularity = input$var_granularity, # NULL or a list of categoricals
aggregation_metric = input$aggregation_metric, # raw, max, min, mean,
time_frequency = time_frequency, # hours, days, weeks, months, quarters, years
graph_type = graph_type, # lines , markers, aread
categoricals = categoricals,
output_type = output_type # c("exploration","forecast","growth_rate")
)
})
report_details <- reactive({
list(
report_dir = report_dir,
report_id = "0001"
)
})
SaldaeReporting::add_to_report_server("add_anomaly", report_details = report_details(),item_elements = reactive({item_elements()}))
#---------------return an output
anomaly_output <- reactive({
req(tisefka_iheggan())
output <- list()
output$tisefka_tizegzawin <- tisefka_iheggan()
return(output)
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.