# A dashboard + demo for segmentation for Jajo Caribbean # Load Libraries shiny, shinydashboard, highcharter, dplyr, tidyr, shinycssloaders,DT library(shiny) library(shinydashboard) library(highcharter) library(dplyr) library(tidyr) library(shinycssloaders) library(DT) setwd(dirname(getwd())) #source helper source("helper.R") #source mod_revenueplot source("mod_revenueplot.R") #source dataimporter source("dataimporter.R") # source mod_datatable.R source("mod_datatable.R") source("mod_scatter.R") source("mod_histo.R") # Define UI for application that draws a histogram ui <- dashboardPage( #Dashboard Title 'Segmentation' dashboardHeader(title = "Segmentation"), #add menu items dashboardSidebar( # sidebarMenu( # menuItem("revenue", tabName = "Revenue", icon = icon("dollar")), # menuItem("segments", tabName = "Segments", icon = icon("users")) # ) ), dashboardBody( # tabItems( # # First tab content # tabItem(tabName = "Revenue", # revplotUI("revplot"), # datatableUI("datatable") # ), # # Second tab content # tabItem(tabName = "Segments", # h2("Segmentation"), # # h4("Traditional Approach - Location"), # # scatterUI("geosegment"), # # h4("Traditional Approach - Role"), # # scatterUI("rolesegment"), # # h4("Machine Learning Approach"), # # scatterUI("rolesegment") # ) # ) revplotUI("revplot"), datatableUI("datatable"), h2("Segmentation"), fluidRow( column(6, h5("Segment visualization"), scatterUI("clustersegment") ), column(6, h5("Revenue by segment"), highchartOutput("revenuebysegment") ) ), h2("Revenue Distribution"), fluidRow( #histoUI inside box box( title = "Segment 1", status = "primary", solidHeader = TRUE, collapsible = F, width = 6, histoUI("segment1") ), box( title = "Segment 2", status = "primary", solidHeader = TRUE, collapsible = F, width = 6, histoUI("segment2") ) ), fluidRow( #histoUI inside box box( title = "Segment 3", status = "primary", solidHeader = TRUE, collapsible = F, width = 6, histoUI("segment3") ), box( title = "Segment 4", status = "primary", solidHeader = TRUE, collapsible = TRUE, width = 6, histoUI("segment4") ) ) ) ) # Define server logic required to draw a histogram server <- function(input, output) { revplotServer("revplot", df = dat, xtitle = "Year", ytitle = "Revenue") datatableServer("datatable", dat[,-10]) scatterServer("clustersegment", dat) histoServer("segment1", dat, segment=1) histoServer("segment2", dat, segment=2) histoServer("segment3", dat, segment=3) histoServer("segment4", dat, segment=4) output$revenuebysegment <- renderHighchart({ dat |> group_by(Cluster) |> summarise( self=sum(Self.Purchase1)+sum(Self.Purchase2)+sum(Self.Purchase3), other=sum(Other.Purchase1)+sum(Other.Purchase2)+sum(Other.Purchase3) ) |> pivot_longer(c(self,other)) |> hchart("column", hcaes(x=Cluster, y=value, group=name), stacking="normal") |> hc_colors(c("#0073C2FF", "#EFC000FF")) |> hc_title(text="Revenue by segment") |> hc_xAxis(title=list(text="Segment")) |> hc_yAxis(title=list(text="Revenue")) }) } # Run the application shinyApp(ui = ui, server = server)