Dears I am running the below code : The problem is that when I switch between "Dashbaord" and "Data" and "Basket Analysis tab" the basket tab is still showing the same data as "Data" Tab below is my script:
Data is about the Ecommerce From this website :https://www.kaggle.com/ostrowski/market-basket-analysis-exploring-e-commercedata/data
library(ggplot2)
library(dplyr)
library(shiny)
library(DT)
library(shinydashboard)
library(reshape2)
library(tidyr)
library(forcats)
library(arules)
library(arulesViz)
## Only run this example in interactive R sessions
if (interactive()) {
ui <- dashboardPage(skin = "blue",
dashboardHeader(title = "E-Commerce"),
dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Data", tabName = "Data", icon = icon("th")),
menuItem("Basket Analysis", tabName = "Basket Analysis", icon = icon("th"))
),
actionButton('switchtab', 'Switch tab')
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
tabItem(tabName = "Dashboard",
fluidRow(
valueBoxOutput("value1"),
valueBoxOutput("value2"),
valueBoxOutput("value3"),
plotOutput("lineChart"),
plotOutput("lineChartQuatity"),
titlePanel("Select a date to check sales by country (exc.United Kingdom)"),
column(4, wellPanel(
dateRangeInput('dateRange',
label = 'Filter Sales by date',
start = min(EcommerceDataClean2$Date) , end = max(EcommerceDataClean2$Date)
)
)),
plotOutput("BarChart")
)
)
),
tabItem(tabName = "Data",
DT::dataTableOutput("dataTable")
)
tabItem(tabName = "Basket Analysis",
fluidPage(
sidebarLayout(sidebarPanel(
selectInput("sup", "select the support value", choices = c(.1, .01, .05, .001, .005)),
selectInput("conf", "select the confidence", choices = c(0.5 , .6 ,.7, .8, .9))
),mainPanel(verbatimTextOutput("mba")))))
)
)
)
server <- function(input, output, session) {
observeEvent(input$switchtab, {
newtab <- switch(input$tabs,
"dashboard" = "Data",
"Data" = "Basket Analysis",
"Basket Analysis" = "dashboard"
)
updateTabItems(session, "tabs", newtab)
})
ECommerce <- read.csv("C:/Education/MSBA - AUB/MSBA - Mario/Summer 2018 - 2019/Data Driven in Digital Marketing/ECommerce.csv")
#------------------------------ Data Cleansing -------------------------#
EcommerceDate <- ECommerce %>%
mutate(Date=as.Date(ECommerce$InvoiceDate, "%m/%d/%Y"))
# create Month year date
EcommerceMonthYear <- EcommerceDate %>%
# mutate(Month_Year=as.Date(EcommerceDate13$Date,format="%Y-%m"))
mutate(Year_Month=format(as.Date(EcommerceDate13$Date,format="%Y-%m-%d"),"%Y-%m"))
# Create the Year field
EcommerceYear <- EcommerceMonthYear%>%
mutate(Year=format(as.Date(EcommerceMonthYear$Date, format="%Y-%m-%d"),"%Y"))
# Remove negative Unit Price
Ecommerce_Cleaning2 <- EcommerceYear %>% filter(UnitPrice >0)
Ecommerce_Cleaning2
# Remove negative Quantity
Ecommerce_Cleaning3 <- Ecommerce_Cleaning2 %>% filter(Quantity >0)
Ecommerce_Cleaning3
# Calculating total price (for example if the customer bought 2 units we should multiply the quantity of unit by the unit price)
Ecommerce_Cleaning <- Ecommerce_Cleaning3 %>%
mutate(invoiceVolume= UnitPrice * Quantity)
Ecommerce_CleanData <- Ecommerce_Cleaning
#some data manipulation to derive the values of KPI boxes
total.Sales <- sum(Ecommerce_CleanData$invoiceVolume)
total.Invoice <- Ecommerce_CleanData %>% dplyr::summarise(n = n())
AveragePrice <- Ecommerce_CleanData %>% dplyr::summarize(Mean = mean(UnitPrice, na.rm=TRUE))
# aggregate data to plot line and bar chart
EcommerceDataClean <- Ecommerce_CleanData %>% group_by(Year_Month) %>% dplyr::summarize(TotalSales = sum(invoiceVolume, na.rm=TRUE))
# Agregation to check total quantity monthly
EcommerceDataQuatity <- Ecommerce_CleanData %>% group_by(Year_Month) %>% dplyr::summarize(TotalOrders = sum(Quantity, na.rm=TRUE))
EcommerceDataClean2 <- Ecommerce_CleanData %>% group_by_(.dots=c("Date","Country")) %>% dplyr::summarize(TotalSales = sum(invoiceVolume, na.rm=TRUE))
EcommerceDataClean3 <- EcommerceDataClean2[order(EcommerceDataClean2$TotalSales,decreasing = TRUE),]%>% filter(Country != "United Kingdom")
Ecommerce_Tabular <- table(Ecommerce_CleanData$InvoiceNo,Ecommerce_CleanData$StockCode) #Invoice# (transactions)-Items ordered
# str(Ecommerce_Tabular)
Ecommerce_Tabular.df <- as.data.frame.matrix(Ecommerce_Tabular) #This step is expected to convert a table to Dataframe
#
#str(Ecommerce_Tabular.df)
Ecommerce_Matrix <- as.matrix(Ecommerce_Tabular.df)
Ecommerce_DuplicateItems <- which((Ecommerce_Matrix[,]>1 | Ecommerce_Matrix[,]<0),arr.ind = TRUE) #lists the invoice with multiple items in the same invoice (array format - arr.ind=TRUE).
Ecommerce_DuplicateItems[1:5,]
# length(which((Ecommerce_Matrix[,]>1),arr.ind = TRUE)) #Transactions with SKU count greater than 1
#length(which((Ecommerce_Matrix[,]<0),arr.ind = TRUE)) #Transactions with SKU count less than 0
Ecommerce_Matrix[which(Ecommerce_Matrix[,]>1 | Ecommerce_Matrix[,]<0)] <- 1
rulesEcommerce <- apriori(Ecommerce_Matrix,parameter = list(supp=0.015,conf = 0.73,target = "rules"))
assocRulesEcommerce <- inspect(head(sort(rulesEcommerce,by="lift"),20))
assocRulesEcommerce <- inspect(head(sort(rulesEcommerce,by="lift"),20))
#Following are the stock description, which would be useful to interpret the rule generated on the 11-month worth invoice data;
#Ecommercehead20 <- head(sort(rulesEcommerce,by="lift"),20) ##capture the rules sorted by "lift" for plotting
#plot(Ecommercehead20,method = "graph")
#-----Define outputs-----#
output$value1 <- renderValueBox({
valueBox(
formatC( total.Sales, format="d", big.mark=',')
,'Total Sales in USD'
,icon = icon("Dollar",lib='glyphicon')
,color = "blue")
})
output$value2 <- renderValueBox({
valueBox(
format(total.Invoice, format="d", big.mark=',')
,'Total Number of Items'
,icon = icon(" ",lib='glyphicon')
,color = "blue")
})
output$value3 <- renderValueBox({
valueBox(
format(AveragePrice, format="d", big.mark=',', digits= "0")
,'Average Price in USD'
,icon = icon(" ",lib='glyphicon')
,color = "blue")
})
output$lineChart <- renderPlot({
ggplot(data=EcommerceDataClean, aes(x=Year_Month, y=TotalSales , group=1))+
geom_line(color="blue") + geom_point() + ggtitle("Total Sales Evolution")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"), axis.title.x=element_blank())
},height = 280
,width = 1500)
output$lineChartQuatity <- renderPlot ({
ggplot(data=EcommerceDataQuatity, aes(x=Year_Month, y=TotalOrders , group=1))+
geom_line(color="blue") + geom_point() + ggtitle("Total Quantity Evolution") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"), axis.title.x=element_blank())
},height = 280
,width = 1500)
output$BarChart <- renderPlot ({
EcommerceBar <- EcommerceDataClean3 %>% filter(Date >= input$dateRange[1] & Date <= input$dateRange[2])
ggplot(EcommerceBar,aes(Country,TotalSales)) +
geom_bar(stat="sum",na.rm=TRUE) + theme(axis.text.x = element_text(angle = 45, hjust = 1 ,vjust = 1)) +
theme(panel.background = element_blank()) + scale_fill_manual(values=c("#4c4c4c"))
},height = 280
,width = 1500)
output$dataTable <- renderDataTable(Ecommerce_CleanData)
}
shinyApp(ui, server)
}
Please Help
Thanks in advance