Yes andresrce,
below is sample code
Thanks for help.
===================================================================
# Sales Data Analysis Report !
#specify the packages of interest
packages = c("dplyr", "ggplot2","gridExtra","shiny","shinydashboard",
"DT", "numform", "shinyBS")
#use this function to check if each package is on the local machine
#if a package is installed, it will be loaded
#if any are not, the missing package(s) will be installed and loaded
package.check <- lapply(packages, FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
})
# Clear the Global Environment
rm(list = ls())
"Sales Data Analysis Report !"
# Clear the Globle Enviroment
rm(list = ls())
ui = dashboardPage(
dashboardHeader(title = HTML(paste0(icon('cubes')),"Sales Data Analysis Report !"),
titleWidth = 310
),
dashboardSidebar(
tags$footer(p("Ganesh Patil"),
p("email@gmail.com"), align = "right", style = "
position:absolute;
bottom:0;
width:100%;
height:50px; /* Height of the footer */
color: white;
padding: 10px;
z-index: 1000;"
),
sidebarMenu(
br(),
menuItem("Dashboard-1",tabName = "dashboard_1",icon = icon("dashboard"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard_1",
fluidPage(
fluidRow(
infoBoxOutput("numb_of_usr"),
infoBoxOutput("numb_of_Prod"),
infoBoxOutput("Total_sale")
),
fluidRow(
box(width = 4,height = 300, title = "Percent of Sales by Gender",status = "primary",
solidHeader = TRUE, collapsible = TRUE,
plotOutput("plot1")),
box(width = 4,height = 300, title = "Sales ($) by Gender",status = "primary",
solidHeader = TRUE, collapsible = TRUE,
plotOutput("plot2")),
box(width = 4,height = 300, title = "Sales ($) by Age Group",status = "primary",
solidHeader = TRUE, collapsible = TRUE,
plotOutput("plot3"))
)
)
)
)
)
)
server = function(input , output){
# Read the csv data file
sale_data =data.table::fread("D:/Study Material/R shiny/Sales Data Analysis/data.csv",
colClasses = c(rep("character", 2),"factor",rep("character", 8),"integer"),
stringsAsFactors = FALSE)
# InfoBox - Distinct Number of Users
output$numb_of_usr = renderInfoBox({
infoBox(
"Number of Users", prettyNum(sale_data%>%distinct(User_ID)%>%count(), big.mark = ","),
icon = icon("address-card"),fill = FALSE
)
})
# InfoBox - Distinct Number of Products (Product Count)
output$numb_of_Prod = renderInfoBox({
infoBox(
"Number of Products", prettyNum(sale_data%>%distinct(Product_ID)%>%count(), big.mark = ","),
color = "purple", icon = icon("list"),fill = FALSE
)
})
# InfoBox - Total Sale
output$Total_sale = renderInfoBox({
infoBox(
"Total Sale ($)", paste0("$", " ", prettyNum(sale_data%>%select(Purchase)%>%summarise(Total = sum(as.numeric(Purchase))), big.mark = ",")),
color = "yellow", icon = icon("thumbs-up", lib = "glyphicon"),fill = FALSE
)
})
# Pie Chart - Percent of Seles by Gender
output$plot1 = renderPlot(width = "auto", height = 230,{
sales_by_gender_pie = sale_data%>%group_by(Gender)%>%summarise(sales_1 = sum(as.numeric(Purchase)))%>%
mutate( per_sale = round((sales_1/sum(sales_1))*100,2))%>%
ggplot( aes(x=factor(1), y=Gender, fill=factor(prettyNum(sales_1,big.mark = ",")))) +
geom_bar(stat="identity") +
geom_text(aes(x= factor(1), label = paste0(Gender, " ", paste0(per_sale, "%"))), size=5) + # note y = pos
# facet_grid(facets = .~Gender, labeller = label_value) +
coord_polar(theta = "y")+
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank())+
theme_void() +
scale_fill_discrete(name = "Sale ($)");
sales_by_gender_pie
})
# Sales ($) by Gender
output$plot2 = renderPlot(width = "auto", height = 230,{
sales_by_gender_bar = sale_data%>%group_by(Gender)%>%summarise(sales_1 = sum(as.numeric(Purchase)))%>%
mutate( per_sale = round((sales_1/sum(sales_1))*100,2))%>%
ggplot( aes(x=Gender, y=sales_1, fill = Gender)) +
geom_bar(stat="identity") +
geom_text(aes(label = prettyNum(sales_1, big.mark = ",")), vjust = 0 ) +
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank()) +
theme_void();
sales_by_gender_bar
})
# Sales by Age group
output$plot3 = renderPlot(width = "auto", height = 230,{
sales_by_age = sale_data%>%group_by(Age,Gender)%>%summarise(sales_by_age = sum(as.numeric(Purchase)))%>%
ggplot(aes(x = Age, y = sales_by_age, fill = Gender)) +
geom_bar(stat = 'identity', position = 'dodge') +
theme_classic();
sales_by_age + theme(legend.position="bottom", axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(y = "Sale ($)")
})
# Sales ($) by City
output$plot4 = renderPlot(width = "auto", height = 230,{
sales_by_city_pie = sale_data%>%group_by(City_Category)%>%summarise(sales_by_city = sum(as.numeric(Purchase)))%>%
mutate( per_sale_city = round((sales_by_city/sum(sales_by_city))*100,2))%>%
ggplot( aes(x=factor(1), y=City_Category, fill=factor(prettyNum(sales_by_city,big.mark = ",")))) +
geom_bar(stat="identity") +
geom_text(aes(x= factor(1), label = paste0(City_Category, " ", paste0(per_sale_city, "%"))), size=5) + # note y = pos
# facet_grid(facets = .~Gender, labeller = label_value) +
coord_polar(theta = "y")+
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank()) +
theme_void() +
scale_fill_discrete(name = "Sale ($)");
sales_by_city_pie
})
}
shinyApp(ui = ui , server = server )
=====================================================================