Asitav Sen 2 years ago
commit
7fd27a0f57
  1. 4
      .gitignore
  2. 130
      Analysis.qmd
  3. 141
      Data.csv
  4. 141
      DataSegmented.csv
  5. 13
      J&J.Rproj
  6. 2
      dataimporter.R
  7. 66
      helper.R
  8. 23
      mod_bar.R
  9. 18
      mod_datatable.R
  10. 41
      mod_histo.R
  11. 30
      mod_infocards.R
  12. 168
      mod_revenueplot.R
  13. 35
      mod_scatter.R

4
.gitignore

@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata

130
Analysis.qmd

@ -0,0 +1,130 @@
---
title: "Analysis"
format: html
editor: visual
---
# Setup
```{r}
library(dplyr)
library(cluster)
library(tidyr)
library(highcharter)
library(ggplot2)
```
# Data
```{r}
dat<-read.csv("Data.csv", colClasses = c("character","factor", "factor",
"numeric","numeric","numeric",
"numeric","numeric","numeric"))
dat[is.na(dat)]<-0
```
```{r}
# Select the columns for clustering (including "Role" and "Location")
selected_cols <- c("Self.Purchase1", "Self.Purchase2", "Self.Purchase3", "Other.Purchase1", "Other.Purchase2", "Other.Purchase3", "Role", "Location")
# Subset the data frame to include only the selected columns
df_subset <- dat[selected_cols]
# Convert the "Role" and "Location" columns to factors (if they are not already)
df_subset$Role <- as.factor(df_subset$Role)
df_subset$Location <- as.factor(df_subset$Location)
# Perform one-hot encoding for "Role" and "Location"
df_encoded <- model.matrix(~Role + Location - 1, data = df_subset) # -1 removes intercept terms
numeric_cols <- dat[, c("Self.Purchase1", "Self.Purchase2", "Self.Purchase3", "Other.Purchase1", "Other.Purchase2", "Other.Purchase3")]
scaled_data <- scale(numeric_cols)
final_data <- cbind(scaled_data, df_encoded)
wss <- numeric(length = 10)
for (i in 1:10) {
kmeans_result <- kmeans(final_data, centers = i, nstart = 10)
wss[i] <- sum(kmeans_result$tot.withinss)
}
plot(1:10, wss, type = "b", xlab = "Number of Clusters", ylab = "Within-cluster Sum of Squares")
```
```{r}
optimal_k <- 4
kmeans_result <- kmeans(final_data, centers = optimal_k, nstart = 10)
dat$Cluster <- kmeans_result$cluster
```
```{r}
summary(dat[dat$Cluster==1,])
filter.curr<-dat |>
filter(Location=="Curacao") |>
summarize(yr.1=sum(Self.Purchase1)+sum(Other.Purchase1),
yr.2=sum(Self.Purchase2)+sum(Other.Purchase2),
yr.3=sum(Self.Purchase3)+sum(Other.Purchase3)
) |>
pivot_longer(c(yr.1,yr.2,yr.3)) |>
rename(Year=name, Revenue=value)
hchart(filter.curr,
"column",
hcaes(x=Year,y=Revenue)) |>
hc_title(text="Revenue from self purchase")
unique(dat$Role)
write.csv(dat,"DataSegmented.csv",row.names = FALSE)
dat |>
summarise(current.year=sum(Self.Purchase3),
last.year=sum(Self.Purchase2))
library(ggplot2)
# Create a scatter plot to visualize the clusters
summ.dat<-dat |>
group_by(Cluster) |>
mutate(
self=sum(Self.Purchase1)+sum(Self.Purchase2)+sum(Self.Purchase3),
other=sum(Other.Purchase1)+sum(Other.Purchase2)+sum(Other.Purchase3)
)
hchart(summ.dat, type = "scatter", hcaes(x = self, y = other, color = factor(Cluster))) %>%
hc_plotOptions(
scatter = list(jitter = list(x = 10000000, y = 10000000))
) %>%
hc_title(text = "K-means Clustering Visualization") %>%
hc_xAxis(title = list(text = "Self")) %>%
hc_yAxis(title = list(text = "Other"))
```
```{r}
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"))
```
```{r}
```

141
Data.csv

@ -0,0 +1,141 @@
ID,Location,Role,Self-Purchase1,Self-Purchase2,Self-Purchase3,Other-Purchase1,Other-Purchase2,Other-Purchase3
C0000001,Curacao,Influencer,,,,6850964,5654457,9406877
C0000002,Aruba,Customer,7131213,9508179,987473,,,
C0000003,Bonair,Financer,,,,548193,1412875,3975250
C0000004,Poland,Customer,2553081,2055717,6042879,,,
C0000005,Netherlands,Customer,6637841,6840146,9248388,,,
C0000006,Bonair,Customer,994470,4169155,8282001,,,
C0000007,Poland,Customer,3560614,3248290,9812875,,,
C0000008,Curacao,Influencer,,,,9413526,5754149,3992776
C0000009,Aruba,Financer,,,,2015637,6405757,9619615
C0000010,Bonair,Influencer,,,,5668934,6369789,1845955
C0000011,Poland,Customer,9609707,744684,7110931,,,
C0000012,Netherlands,Customer,4654890,1917786,550357,,,
C0000013,Bonair,Influencer,,,,1312206,5466686,3908688
C0000014,Poland,Financer,,,,5855506,9427638,4608109
C0000015,Curacao,Customer,9047470,9559322,9436105,,,
C0000016,Netherlands,Customer,5361462,6550334,1515016,,,
C0000017,Bonair,Customer,4661060,5797952,5674981,,,
C0000018,Poland,Influencer,,,,7479391,8289392,9167178
C0000019,Curacao,Influencer,,,,2309204,7493262,2141330
C0000020,Aruba,Customer,9456756,4729160,6832637,,,
C0000021,Bonair,Customer,5853143,8525172,8887826,,,
C0000022,Poland,Customer,5370322,3798484,104674,,,
C0000023,Curacao,Customer,2774251,9562833,3527185,,,
C0000024,Aruba,Customer - Influencer,8111858,3912852,6489338,8158659,5765951,2792040
C0000025,Bonair,Influencer,,,,8105900,8476122,888443
C0000026,Poland,Customer - Influencer,4326190,745375,1848947,4215999,7245153,5634095
C0000027,Netherlands,Customer,8359655,9116334,4392579,,,
C0000028,Bonair,Financer,,,,8923166,9864806,9645095
C0000029,Poland,Customer,5893730,1380999,126034,,,
C0000030,Curacao,Customer - Influencer,4723801,4875711,1384839,5282297,3808924,9393130
C0000031,Aruba,Customer,1841077,9819493,2180732,,,
C0000032,Bonair,Customer - Influencer,964591,6754659,23510,4486248,5627199,7444388
C0000033,Poland,Customer,8548256,1460621,993947,,,
C0000034,Netherlands,Influencer,,,,5755340,1580680,4972571
C0000035,Bonair,Customer,2437760,3191899,9552521,,,
C0000036,Poland,Customer - Influencer,5050311,3949725,1029525,2312624,6444301,7401120
C0000037,Curacao,Customer,1028795,7396457,9620996,,,
C0000038,Netherlands,Customer,7474674,9635103,1018872,,,
C0000039,Bonair,Influencer,,,,6973166,6839840,6039936
C0000040,Poland,Customer,5309502,3184398,7762433,,,
C0000041,Curacao,Customer,7244003,5023465,6982327,,,
C0000042,Aruba,Customer - Influencer,7045724,5735292,765384,3900048,9255434,677199
C0000043,Bonair,Customer - Influencer,5065160,1735502,5624926,5270681,6939135,2241408
C0000044,Poland,Influencer,,,,8154524,4865645,343141
C0000045,Curacao,Customer,6527461,5422097,7453798,,,
C0000046,Aruba,Customer,6789299,3508214,6217066,,,
C0000047,Bonair,Customer,6372242,5665016,2274022,,,
C0000048,Poland,Customer,5534358,9425294,9225570,,,
C0000049,Netherlands,Customer,3061151,8277981,8337830,,,
C0000050,Bonair,Customer,2407559,679527,4743749,,,
C0000051,Poland,Influencer,,,,3469082,3829784,2846226
C0000052,Curacao,Customer,2917953,9968850,8655367,,,
C0000053,Aruba,Customer - Influencer,6088957,6809652,1494989,1160005,9517103,2669539
C0000054,Bonair,Customer,7696706,9368178,5098787,,,
C0000055,Poland,Customer,5431700,4248314,430067,,,
C0000056,Netherlands,Customer,5722694,4202463,6763881,,,
C0000057,Bonair,Customer - Influencer,8377,7586630,6500191,5456722,9768140,9671315
C0000058,Poland,Customer - Influencer,553221,6444260,6872714,937520,8697475,1466567
C0000059,Curacao,Financer,,,,6010226,1769220,5483324
C0000060,Netherlands,Financer,,,,5893243,6343427,3984191
C0000061,Bonair,Influencer,,,,1657152,2427771,8056730
C0000062,Poland,Influencer,,,,5683670,4636921,8230018
C0000063,Curacao,Influencer,,,,6258416,4968085,5576340
C0000064,Aruba,Influencer,,,,6050700,63010,2722225
C0000065,Bonair,Customer,2910659,7308232,5539026,,,
C0000066,Poland,Customer,1223566,1395405,2139269,,,
C0000067,Curacao,Customer,63008,1257997,7461056,,,
C0000068,Aruba,Customer,4390493,8258654,3431932,,,
C0000069,Bonair,Customer,5164155,8529350,5578076,,,
C0000070,Poland,Customer - Influencer,8167303,4549196,4785271,9755818,9740102,6596398
C0000071,Curacao,Influencer,,,,18250,59318,58223
C0000072,Aruba,Customer,84911,81540,65248,,,
C0000073,Bonair,Financer,,,,84484,78135,83685
C0000074,Poland,Customer,6470,81231,59125,,,
C0000075,Netherlands,Customer,63448,61909,3307,,,
C0000076,Bonair,Customer,4901,48484,14396,,,
C0000077,Poland,Customer,49894,68761,99808,,,
C0000078,Curacao,Influencer,,,,98147,29273,96396
C0000079,Aruba,Financer,,,,12947,24849,69733
C0000080,Bonair,Influencer,,,,54188,94693,60807
C0000081,Poland,Customer,2009,97777,71655,,,
C0000082,Netherlands,Customer,74469,68144,19264,,,
C0000083,Bonair,Influencer,,,,53241,22413,4772
C0000084,Poland,Financer,,,,87700,41719,13391
C0000085,Curacao,Customer,95426,16469,37456,,,
C0000086,Netherlands,Customer,88462,74096,37267,,,
C0000087,Bonair,Customer,68149,92251,59055,,,
C0000088,Poland,Influencer,,,,34231,91907,23849
C0000089,Curacao,Influencer,,,,41402,59644,90552
C0000090,Aruba,Customer,90252,51233,833,,,
C0000091,Bonair,Customer,99898,81887,79617,,,
C0000092,Poland,Customer,56891,77610,2680,,,
C0000093,Curacao,Customer,20291,82944,79205,,,
C0000094,Aruba,Customer - Influencer,19676,59970,11747,79145,135,9750
C0000095,Bonair,Influencer,,,,55469,14512,38968
C0000096,Poland,Customer - Influencer,75717,41077,6640,90462,7002,63317
C0000097,Netherlands,Customer,54838,7955,30895,,,
C0000098,Bonair,Financer,,,,67698,32278,78655
C0000099,Poland,Customer,54751,92309,66826,,,
C0000100,Curacao,Customer - Influencer,27104,30660,27863,7274,87988,92211
C0000101,Aruba,Customer,14237,71522,26663,,,
C0000102,Bonair,Customer - Influencer,93454,54131,83377,25541,2289,58661
C0000103,Poland,Customer,67826,55642,40465,,,
C0000104,Netherlands,Influencer,,,,72837,84915,12127
C0000105,Bonair,Customer,64381,92814,42961,,,
C0000106,Poland,Customer - Influencer,88797,91280,32675,37215,34484,25238
C0000107,Curacao,Customer,24376,15913,88949,,,
C0000108,Netherlands,Customer,39993,64896,99752,,,
C0000109,Bonair,Influencer,,,,98551,15421,30879
C0000110,Poland,Customer,22666,75782,63663,,,
C0000111,Curacao,Customer,89194,4653,65621,,,
C0000112,Aruba,Customer - Influencer,58267,85604,12283,51236,87049,55382
C0000113,Bonair,Customer - Influencer,62438,68614,5373,22700,69948,608
C0000114,Poland,Influencer,,,,48226,89069,72941
C0000115,Curacao,Customer,58277,71778,31537,,,
C0000116,Aruba,Customer,51367,79050,38842,,,
C0000117,Bonair,Customer,24399,91985,27110,,,
C0000118,Poland,Customer,54135,5712,97775,,,
C0000119,Netherlands,Customer,29426,22873,74479,,,
C0000120,Bonair,Customer,99494,68286,87738,,,
C0000121,Poland,Influencer,,,,9489,38275,16109
C0000122,Curacao,Customer,36035,86335,98167,,,
C0000123,Aruba,Customer - Influencer,23085,53627,85483,97542,395,66481
C0000124,Bonair,Customer,44598,45319,34112,,,
C0000125,Poland,Customer,15802,86528,9973,,,
C0000126,Netherlands,Customer,8873,63994,7273,,,
C0000127,Bonair,Customer - Influencer,61438,61137,79315,81888,94231,85669
C0000128,Poland,Customer - Influencer,97913,79735,75865,46181,98345,16404
C0000129,Curacao,Financer,,,,63005,4072,52167
C0000130,Netherlands,Financer,,,,50455,57128,90845
C0000131,Bonair,Influencer,,,,13252,33196,24522
C0000132,Poland,Influencer,,,,36931,53902,52130
C0000133,Curacao,Influencer,,,,17085,59466,94952
C0000134,Aruba,Influencer,,,,18693,10233,69228
C0000135,Bonair,Customer,86034,74120,1581,,,
C0000136,Poland,Customer,54549,27123,58848,,,
C0000137,Curacao,Customer,97680,48236,31164,,,
C0000138,Aruba,Customer,48471,67454,10603,,,
C0000139,Bonair,Customer,61078,79854,83893,,,
C0000140,Poland,Customer - Influencer,20022,96206,66039,42597,76718,60157

141
DataSegmented.csv

@ -0,0 +1,141 @@
"ID","Location","Role","Self.Purchase1","Self.Purchase2","Self.Purchase3","Other.Purchase1","Other.Purchase2","Other.Purchase3","Cluster"
"C0000001","Curacao","Influencer",0,0,0,6850964,5654457,9406877,2
"C0000002","Aruba","Customer",7131213,9508179,987473,0,0,0,1
"C0000003","Bonair","Financer",0,0,0,548193,1412875,3975250,4
"C0000004","Poland","Customer",2553081,2055717,6042879,0,0,0,1
"C0000005","Netherlands","Customer",6637841,6840146,9248388,0,0,0,1
"C0000006","Bonair","Customer",994470,4169155,8282001,0,0,0,1
"C0000007","Poland","Customer",3560614,3248290,9812875,0,0,0,1
"C0000008","Curacao","Influencer",0,0,0,9413526,5754149,3992776,2
"C0000009","Aruba","Financer",0,0,0,2015637,6405757,9619615,2
"C0000010","Bonair","Influencer",0,0,0,5668934,6369789,1845955,2
"C0000011","Poland","Customer",9609707,744684,7110931,0,0,0,1
"C0000012","Netherlands","Customer",4654890,1917786,550357,0,0,0,4
"C0000013","Bonair","Influencer",0,0,0,1312206,5466686,3908688,2
"C0000014","Poland","Financer",0,0,0,5855506,9427638,4608109,2
"C0000015","Curacao","Customer",9047470,9559322,9436105,0,0,0,1
"C0000016","Netherlands","Customer",5361462,6550334,1515016,0,0,0,1
"C0000017","Bonair","Customer",4661060,5797952,5674981,0,0,0,1
"C0000018","Poland","Influencer",0,0,0,7479391,8289392,9167178,2
"C0000019","Curacao","Influencer",0,0,0,2309204,7493262,2141330,2
"C0000020","Aruba","Customer",9456756,4729160,6832637,0,0,0,1
"C0000021","Bonair","Customer",5853143,8525172,8887826,0,0,0,1
"C0000022","Poland","Customer",5370322,3798484,104674,0,0,0,1
"C0000023","Curacao","Customer",2774251,9562833,3527185,0,0,0,1
"C0000024","Aruba","Customer - Influencer",8111858,3912852,6489338,8158659,5765951,2792040,3
"C0000025","Bonair","Influencer",0,0,0,8105900,8476122,888443,2
"C0000026","Poland","Customer - Influencer",4326190,745375,1848947,4215999,7245153,5634095,3
"C0000027","Netherlands","Customer",8359655,9116334,4392579,0,0,0,1
"C0000028","Bonair","Financer",0,0,0,8923166,9864806,9645095,2
"C0000029","Poland","Customer",5893730,1380999,126034,0,0,0,4
"C0000030","Curacao","Customer - Influencer",4723801,4875711,1384839,5282297,3808924,9393130,3
"C0000031","Aruba","Customer",1841077,9819493,2180732,0,0,0,1
"C0000032","Bonair","Customer - Influencer",964591,6754659,23510,4486248,5627199,7444388,3
"C0000033","Poland","Customer",8548256,1460621,993947,0,0,0,1
"C0000034","Netherlands","Influencer",0,0,0,5755340,1580680,4972571,2
"C0000035","Bonair","Customer",2437760,3191899,9552521,0,0,0,1
"C0000036","Poland","Customer - Influencer",5050311,3949725,1029525,2312624,6444301,7401120,3
"C0000037","Curacao","Customer",1028795,7396457,9620996,0,0,0,1
"C0000038","Netherlands","Customer",7474674,9635103,1018872,0,0,0,1
"C0000039","Bonair","Influencer",0,0,0,6973166,6839840,6039936,2
"C0000040","Poland","Customer",5309502,3184398,7762433,0,0,0,1
"C0000041","Curacao","Customer",7244003,5023465,6982327,0,0,0,1
"C0000042","Aruba","Customer - Influencer",7045724,5735292,765384,3900048,9255434,677199,3
"C0000043","Bonair","Customer - Influencer",5065160,1735502,5624926,5270681,6939135,2241408,3
"C0000044","Poland","Influencer",0,0,0,8154524,4865645,343141,2
"C0000045","Curacao","Customer",6527461,5422097,7453798,0,0,0,1
"C0000046","Aruba","Customer",6789299,3508214,6217066,0,0,0,1
"C0000047","Bonair","Customer",6372242,5665016,2274022,0,0,0,1
"C0000048","Poland","Customer",5534358,9425294,9225570,0,0,0,1
"C0000049","Netherlands","Customer",3061151,8277981,8337830,0,0,0,1
"C0000050","Bonair","Customer",2407559,679527,4743749,0,0,0,4
"C0000051","Poland","Influencer",0,0,0,3469082,3829784,2846226,2
"C0000052","Curacao","Customer",2917953,9968850,8655367,0,0,0,1
"C0000053","Aruba","Customer - Influencer",6088957,6809652,1494989,1160005,9517103,2669539,3
"C0000054","Bonair","Customer",7696706,9368178,5098787,0,0,0,1
"C0000055","Poland","Customer",5431700,4248314,430067,0,0,0,1
"C0000056","Netherlands","Customer",5722694,4202463,6763881,0,0,0,1
"C0000057","Bonair","Customer - Influencer",8377,7586630,6500191,5456722,9768140,9671315,3
"C0000058","Poland","Customer - Influencer",553221,6444260,6872714,937520,8697475,1466567,3
"C0000059","Curacao","Financer",0,0,0,6010226,1769220,5483324,2
"C0000060","Netherlands","Financer",0,0,0,5893243,6343427,3984191,2
"C0000061","Bonair","Influencer",0,0,0,1657152,2427771,8056730,2
"C0000062","Poland","Influencer",0,0,0,5683670,4636921,8230018,2
"C0000063","Curacao","Influencer",0,0,0,6258416,4968085,5576340,2
"C0000064","Aruba","Influencer",0,0,0,6050700,63010,2722225,2
"C0000065","Bonair","Customer",2910659,7308232,5539026,0,0,0,1
"C0000066","Poland","Customer",1223566,1395405,2139269,0,0,0,4
"C0000067","Curacao","Customer",63008,1257997,7461056,0,0,0,4
"C0000068","Aruba","Customer",4390493,8258654,3431932,0,0,0,1
"C0000069","Bonair","Customer",5164155,8529350,5578076,0,0,0,1
"C0000070","Poland","Customer - Influencer",8167303,4549196,4785271,9755818,9740102,6596398,3
"C0000071","Curacao","Influencer",0,0,0,18250,59318,58223,4
"C0000072","Aruba","Customer",84911,81540,65248,0,0,0,4
"C0000073","Bonair","Financer",0,0,0,84484,78135,83685,4
"C0000074","Poland","Customer",6470,81231,59125,0,0,0,4
"C0000075","Netherlands","Customer",63448,61909,3307,0,0,0,4
"C0000076","Bonair","Customer",4901,48484,14396,0,0,0,4
"C0000077","Poland","Customer",49894,68761,99808,0,0,0,4
"C0000078","Curacao","Influencer",0,0,0,98147,29273,96396,4
"C0000079","Aruba","Financer",0,0,0,12947,24849,69733,4
"C0000080","Bonair","Influencer",0,0,0,54188,94693,60807,4
"C0000081","Poland","Customer",2009,97777,71655,0,0,0,4
"C0000082","Netherlands","Customer",74469,68144,19264,0,0,0,4
"C0000083","Bonair","Influencer",0,0,0,53241,22413,4772,4
"C0000084","Poland","Financer",0,0,0,87700,41719,13391,4
"C0000085","Curacao","Customer",95426,16469,37456,0,0,0,4
"C0000086","Netherlands","Customer",88462,74096,37267,0,0,0,4
"C0000087","Bonair","Customer",68149,92251,59055,0,0,0,4
"C0000088","Poland","Influencer",0,0,0,34231,91907,23849,4
"C0000089","Curacao","Influencer",0,0,0,41402,59644,90552,4
"C0000090","Aruba","Customer",90252,51233,833,0,0,0,4
"C0000091","Bonair","Customer",99898,81887,79617,0,0,0,4
"C0000092","Poland","Customer",56891,77610,2680,0,0,0,4
"C0000093","Curacao","Customer",20291,82944,79205,0,0,0,4
"C0000094","Aruba","Customer - Influencer",19676,59970,11747,79145,135,9750,4
"C0000095","Bonair","Influencer",0,0,0,55469,14512,38968,4
"C0000096","Poland","Customer - Influencer",75717,41077,6640,90462,7002,63317,4
"C0000097","Netherlands","Customer",54838,7955,30895,0,0,0,4
"C0000098","Bonair","Financer",0,0,0,67698,32278,78655,4
"C0000099","Poland","Customer",54751,92309,66826,0,0,0,4
"C0000100","Curacao","Customer - Influencer",27104,30660,27863,7274,87988,92211,4
"C0000101","Aruba","Customer",14237,71522,26663,0,0,0,4
"C0000102","Bonair","Customer - Influencer",93454,54131,83377,25541,2289,58661,4
"C0000103","Poland","Customer",67826,55642,40465,0,0,0,4
"C0000104","Netherlands","Influencer",0,0,0,72837,84915,12127,4
"C0000105","Bonair","Customer",64381,92814,42961,0,0,0,4
"C0000106","Poland","Customer - Influencer",88797,91280,32675,37215,34484,25238,4
"C0000107","Curacao","Customer",24376,15913,88949,0,0,0,4
"C0000108","Netherlands","Customer",39993,64896,99752,0,0,0,4
"C0000109","Bonair","Influencer",0,0,0,98551,15421,30879,4
"C0000110","Poland","Customer",22666,75782,63663,0,0,0,4
"C0000111","Curacao","Customer",89194,4653,65621,0,0,0,4
"C0000112","Aruba","Customer - Influencer",58267,85604,12283,51236,87049,55382,4
"C0000113","Bonair","Customer - Influencer",62438,68614,5373,22700,69948,608,4
"C0000114","Poland","Influencer",0,0,0,48226,89069,72941,4
"C0000115","Curacao","Customer",58277,71778,31537,0,0,0,4
"C0000116","Aruba","Customer",51367,79050,38842,0,0,0,4
"C0000117","Bonair","Customer",24399,91985,27110,0,0,0,4
"C0000118","Poland","Customer",54135,5712,97775,0,0,0,4
"C0000119","Netherlands","Customer",29426,22873,74479,0,0,0,4
"C0000120","Bonair","Customer",99494,68286,87738,0,0,0,4
"C0000121","Poland","Influencer",0,0,0,9489,38275,16109,4
"C0000122","Curacao","Customer",36035,86335,98167,0,0,0,4
"C0000123","Aruba","Customer - Influencer",23085,53627,85483,97542,395,66481,4
"C0000124","Bonair","Customer",44598,45319,34112,0,0,0,4
"C0000125","Poland","Customer",15802,86528,9973,0,0,0,4
"C0000126","Netherlands","Customer",8873,63994,7273,0,0,0,4
"C0000127","Bonair","Customer - Influencer",61438,61137,79315,81888,94231,85669,4
"C0000128","Poland","Customer - Influencer",97913,79735,75865,46181,98345,16404,4
"C0000129","Curacao","Financer",0,0,0,63005,4072,52167,4
"C0000130","Netherlands","Financer",0,0,0,50455,57128,90845,4
"C0000131","Bonair","Influencer",0,0,0,13252,33196,24522,4
"C0000132","Poland","Influencer",0,0,0,36931,53902,52130,4
"C0000133","Curacao","Influencer",0,0,0,17085,59466,94952,4
"C0000134","Aruba","Influencer",0,0,0,18693,10233,69228,4
"C0000135","Bonair","Customer",86034,74120,1581,0,0,0,4
"C0000136","Poland","Customer",54549,27123,58848,0,0,0,4
"C0000137","Curacao","Customer",97680,48236,31164,0,0,0,4
"C0000138","Aruba","Customer",48471,67454,10603,0,0,0,4
"C0000139","Bonair","Customer",61078,79854,83893,0,0,0,4
"C0000140","Poland","Customer - Influencer",20022,96206,66039,42597,76718,60157,4

13
J&J.Rproj

@ -0,0 +1,13 @@
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX

2
dataimporter.R

@ -0,0 +1,2 @@
dat<-read.csv("DataSegmented.csv")

66
helper.R

@ -0,0 +1,66 @@
# Function to summarize & reshape data
summarizer <- function(dat, location, role, self=TRUE){
if(self==TRUE){
filter.curr<-
dat |>
filter(Location %in% location) |>
filter(Role %in% role) |>
summarize(year.1=sum(Self.Purchase1),
year.2=sum(Self.Purchase2),
year.3=sum(Self.Purchase3)
) |>
pivot_longer(c(year.1,year.2,year.3)) |>
rename(Year=name, Revenue=value)
}
if(self==FALSE){
filter.curr<-
dat |>
filter(Location %in% location) |>
filter(Role %in% role) |>
summarize(
year.1=sum(Other.Purchase1),
year.2=sum(Other.Purchase2),
year.3=sum(Other.Purchase3)
) |>
pivot_longer(c(year.1,year.2,year.3)) |>
rename(Year=name, Revenue=value)
}
return(filter.curr)
}
# Snapshot
snapshot<- function(dat, location, role, item="self"){
if(item=="self"){
filter.curr<-
dat |>
filter(Location %in% location) |>
filter(Role %in% role) |>
summarise(current.year=sum(Self.Purchase3),
last.year=sum(Self.Purchase2))
}
if(item=="other"){
filter.curr<-
dat |>
filter(Location %in% location) |>
filter(Role %in% role) |>
summarise(current.year=sum(Other.Purchase3),
last.year=sum(Other.Purchase2))
}
if(item=="total"){
filter.curr<-
dat |>
filter(Location %in% location) |>
filter(Role %in% role) |>
summarise(current.year=sum(Other.Purchase3)+sum(Self.Purchase3),
last.year=sum(Other.Purchase2)+sum(Self.Purchase2))
}
return(filter.curr)
}

23
mod_bar.R

@ -0,0 +1,23 @@
# Module to create a stacked bar chart in highcharter
barplotUI<-function(id){
ns<-NS(id)
tagList(
h4("Cluster"),
highchartOutput(ns("barplot"))
)
}
barplotServer<-function(id, data){
moduleServer(id, function(input, output, session){
output$barplot<-renderHighchart({
data|>
group_by(Cluster)|>
summarise(n=n())|>
hchart("bar", hcaes(x=Cluster, y=n))|>
hc_plotOptions(bar=list(stacking="normal"))
})
})
}

18
mod_datatable.R

@ -0,0 +1,18 @@
# SHiny module to show data frame using DT
datatableUI<- function(id){
ns <- NS(id)
tagList(
h4("Full Data"),
DT::dataTableOutput(ns("datatable"))
)
}
datatableServer<- function(id, df){
moduleServer(id, function(input, output, session){
output$datatable <- DT::renderDataTable({
DT::datatable(df)
})
})
}

41
mod_histo.R

@ -0,0 +1,41 @@
# Module to plot histogram using highcharter
histoUI<-function(id){
ns<-NS(id)
tagList(
selectizeInput(ns("selreve"), "Select", choices = c("self","other"), selected=c("self")),
highchartOutput(ns("histoplot"))
)
}
# Moduleserver
#Server
histoServer<-function(id, df, segment){
moduleServer(id, function(input, output, session){
output$histoplot<-renderHighchart({
summ.dat<- df |>
filter(Cluster==segment) |>
mutate(
self=Self.Purchase1+Self.Purchase2+Self.Purchase3,
other=Other.Purchase1+Other.Purchase2+Other.Purchase3
)
if(input$selreve=="self"){
revenue<-summ.dat$self
}
if(input$selreve=="other"){
revenue<-summ.dat$other
}
#create histogram
hchart(revenue) |>
hc_title(text = "Revenue Distribution") |>
hc_xAxis(title = list(text = "Revenue")) |>
hc_yAxis(title = list(text = "Count"))
})
})
}

30
mod_infocards.R

@ -0,0 +1,30 @@
infocardUI<- function(id){
ns<- NS(id)
uiOutput(ns("infocard"))
}
infocardServer<- function(id, Value, Title="", Stat=NULL, Description="", Icon, Icon_bg="default", BGC){
moduleServer(
id,
function(input, output, session){
output$infocard<- renderUI({
req(Value,Stat)
argonInfoCard(
value=Value,
title = Title,
stat = Stat,
stat_icon = NULL,
description = Description,
icon=Icon,
icon_background = Icon_bg,
hover_lift = TRUE,
shadow = FALSE,
background_color = BGC,
gradient = TRUE,
width = 12
)
})
}
)
}

168
mod_revenueplot.R

@ -0,0 +1,168 @@
revplotUI<- function(id,filtervalues){
ns<- NS(id)
tagList(
fluidRow(
# Add to select inputs side by side
column(width = 3,
selectizeInput(inputId = ns("geo"), label="Select Geography", choices= c("Aruba","Bonair","Poland",
"Curacao", "Netherlands"),
selected=c("Aruba","Bonair","Poland",
"Curacao", "Netherlands"), multiple=T)),
column(width = 3,
selectizeInput(inputId = ns("role"), label="Select Role", choices= c("Customer","Customer - Influencer","Poland",
"Financer", "Influencer"),
c("Customer","Customer - Influencer","Poland",
"Financer", "Influencer"), multiple=T)),
column(width = 6,
shinycssloaders::withSpinner(valueBoxOutput(ns("selfinfo"))),
shinycssloaders::withSpinner(valueBoxOutput(ns("othinfo"))),
shinycssloaders::withSpinner(valueBoxOutput(ns("totinfo"))),
)
),
fluidRow(
column(width = 4,
shinycssloaders::withSpinner(highchartOutput(ns("revselfbar")) , image = "https://media.giphy.com/media/RLsct1jsRVsVFupW7a/giphy.gif", image.height ="200px",hide.ui = T)
),
column(width = 4,
shinycssloaders::withSpinner(highchartOutput(ns("revothbar")) , image = "https://media.giphy.com/media/RLsct1jsRVsVFupW7a/giphy.gif", image.height ="200px",hide.ui = T)
),
column(width = 4,
shinycssloaders::withSpinner(highchartOutput(ns("revtotbar")) , image = "https://media.giphy.com/media/RLsct1jsRVsVFupW7a/giphy.gif", image.height ="200px",hide.ui = T)
)
)
)
}
revplotServer<- function(id, df, xtitle="", ytitle=""){
moduleServer(
id,
function(input, output, session){
sel.geo<-reactive({
print(input$geo)
input$geo
})
sel.role<-reactive({
print(input$role)
input$role
})
summarized.df <- reactive(summarizer(df, sel.geo(), sel.role(), self=TRUE))
summarized.df.oth <- reactive(summarizer(df, sel.geo(), sel.role(), self=FALSE))
output$revselfbar<- renderHighchart({
print(summarized.df())
hchart(summarized.df(),
"column",
hcaes(x=Year,y=Revenue)) |>
hc_title(text="Revenue from self purchase") |>
# hc_yAxis(title = list(text = ytitle)) |>
#hc_plotOptions(column = list(stacking = "normal")) |>
hc_tooltip(shared = TRUE, valueDecimals = 0) |>
#hc_brush(enabled = TRUE) |>
hc_credits(enabled = FALSE) |>
hc_exporting(enabled = TRUE)
}
)
output$revothbar<- renderHighchart({
print(summarized.df.oth())
hchart(summarized.df.oth(),
"column",
hcaes(x=Year,y=Revenue)) |>
hc_title(text="Revenue from purchase by influencer") |>
# hc_yAxis(title = list(text = ytitle)) |>
#hc_plotOptions(column = list(stacking = "normal")) |>
hc_tooltip(shared = TRUE, valueDecimals = 0) |>
#hc_brush(enabled = TRUE) |>
hc_credits(enabled = FALSE) |>
hc_exporting(enabled = TRUE)
}
)
output$revtotbar<- renderHighchart({
summarized.df.tot<-df |>
filter(Location %in% sel.geo()) |>
filter(Role %in% sel.role()) |>
summarize(yr.1=sum(Self.Purchase1)+sum(Other.Purchase1),
yr.2=sum(Self.Purchase2)+sum(Other.Purchase2),
yr.3=sum(Self.Purchase3)+sum(Other.Purchase3)
) |>
pivot_longer(c(yr.1,yr.2,yr.3)) |>
rename(Year=name, Revenue=value)
hchart(summarized.df.tot,
"column",
hcaes(x=Year,y=Revenue)) |>
hc_title(text="Total Revenue") |>
# hc_yAxis(title = list(text = ytitle)) |>
#hc_plotOptions(column = list(stacking = "normal")) |>
hc_tooltip(shared = TRUE, valueDecimals = 0) |>
#hc_brush(enabled = TRUE) |>
hc_credits(enabled = FALSE) |>
hc_exporting(enabled = TRUE)
}
)
selfinfo.df<- reactive({
snapshot(df, sel.geo(), sel.role(), item="self")
})
otherinfo.df<- reactive({
snapshot(df, sel.geo(), sel.role(), item="other")
})
totalinfo.df<- reactive({
snapshot(df, sel.geo(), sel.role(), item="total")
})
output$selfinfo<- renderValueBox({
delta<-round((selfinfo.df()$current.year-selfinfo.df()$last.year)*100/selfinfo.df()$last.year)
if(delta<0){
icon<-icon("arrow-down")
} else {
icon<-icon("arrow-up")
}
valueBox(
value = paste0(delta," %"),
subtitle = "Self",
icon = icon,
color = ifelse(delta>0, "green", "red")
)
})
output$othinfo<- renderValueBox({
delta<-round((otherinfo.df()$current.year-otherinfo.df()$last.year)*100/otherinfo.df()$last.year)
if(delta<0){
icon<-icon("arrow-down")
} else {
icon<-icon("arrow-up")
}
valueBox(
value = paste0(delta," %"),
subtitle = "Other",
icon = icon,
color = ifelse(delta>0, "green", "red")
)
})
output$totinfo<- renderValueBox({
delta<-round((totalinfo.df()$current.year-totalinfo.df()$last.year)*100/totalinfo.df()$last.year)
if(delta<0){
icon<-icon("arrow-down")
} else {
icon<-icon("arrow-up")
}
valueBox(
value = paste0(delta," %"),
subtitle = "Total",
icon = icon,
color = ifelse(delta>0, "green", "red")
)
})
}
)
}

35
mod_scatter.R

@ -0,0 +1,35 @@
# Shiny Module to show scatter plot using highcharter
scatterUI<-function(id){
ns<-NS(id)
tagList(
highchartOutput(ns("scatterPlot"))
)
}
# Server
scatterServer<-function(id,df){
moduleServer(id,function(input,output,session){
output$scatterPlot<-renderHighchart({
summ.dat<-df |>
group_by(Cluster) |>
mutate(
self=sum(Self.Purchase1)+sum(Self.Purchase2)+sum(Self.Purchase3),
other=sum(Other.Purchase1)+sum(Other.Purchase2)+sum(Other.Purchase3)
)
hchart(summ.dat, type = "scatter", hcaes(x = self, y = other, color = factor(Cluster))) |>
hc_plotOptions(
scatter = list(jitter = list(x = 10000000, y = 10000000))
) |>
hc_title(text = "K-means Clustering Visualization") |>
hc_xAxis(title = list(text = "Self")) |>
hc_yAxis(title = list(text = "Other")) |>
# add Cluster as hover info
hc_tooltip(pointFormat = "Cluster: {point.Cluster}")
})
}
)
}
Loading…
Cancel
Save