-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
175 lines (137 loc) · 5.99 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# server.R
# FIT5147 Data Visualization Assessment 4
# Jason Franks
# Monash Student No:28849892
# Uncomment to install required packages:
# install.packages("shiny")
# install.packages("ggplot2")
# install.packages("data.table")
# install.packages("dplyr")
# install.packages("stringr")
# install.packages("scales")
library(shiny)
library(ggplot2)
library(data.table)
library(dplyr)
library(stringr)
library(scales)
# include other code modules
source( file="publisherPlots.R")
source( file="titlePlots.R")
source( file="creativeTeamPlots.R")
source( file="creatorPlots.R")
# Start by loading data globally. We will share this for all server projects
top100GNs2017 <- read.csv("diamonddata_gn_clean.csv")
top100GNs2017$ITEM.CODE <- as.character(top100GNs2017$ITEM.CODE )
top100GNs2017$DESCRIPTION <- as.character(top100GNs2017$DESCRIPTION )
top100GNs2017$CreativeTeam <- as.character(top100GNs2017$CreativeTeam )
top100GNs2017$Writer <- as.character(top100GNs2017$Writer )
top100GNs2017$InteriorArtist <- as.character(top100GNs2017$InteriorArtist )
top100GNs2017$CoverArtist <- as.character(top100GNs2017$CoverArtist )
top100GNs2017$Month <- as.Date(top100GNs2017$Month )
top100GNs2017$WriterIsArtist <- as.logical(top100GNs2017$WriterIsArtist )
top100GNs2017$WriterIsCoverArtist <- as.logical(top100GNs2017$WriterIsCoverArtist )
top100GNs2017$ArtistIsCoverArtist <- as.logical(top100GNs2017$ArtistIsCoverArtist )
top100GNs2017$CreatorOwned <- as.logical(top100GNs2017$CreatorOwned )
top100GNs2017$Creators <- paste(top100GNs2017$Writer, top100GNs2017$InteriorArtist, top100GNs2017$CoverArtist, sep=",")
# Now define the server
shinyServer(function(input, output) {
# Reactive expression to generate the requested filtered data set.
# Otherwise we have to do this for every plot.
currentDF <- reactive({
retval <- top100GNs2017
if( input$titleType == "wfh" )
{
retval <- top100GNs2017[top100GNs2017$CreatorOwned == FALSE,]
}
else if( input$titleType == "creatorOwned" )
{
retval <- top100GNs2017[top100GNs2017$CreatorOwned == TRUE,]
}
retval <- retval[retval$VENDOR %in% input$publishers,]
return(retval)
})
# Another reactive expression, to get an unpacked list of all
# creators and their roles.
currentCreators <- reactive({
# Get the filtered data
workingDF <- currentDF()
if( nrow( workingDF) == 0)
{
df <- data.frame(matrix(ncol = 5, nrow = 0))
colnames(df) <- c("Name", "Writer", "InteriorArtist", "CoverArtist", "role" )
return( df)
}
# wrap in data table
DT <- as.data.table(workingDF)
# Unlist all of the comma-separated writers in the Writers field and pivot into rows, keeping INDEX and ITEM.CODE fields
DTWriters = {
DT[, .(Name = unlist(strsplit(as.character(Writer), ",", fixed = TRUE)),Writer=1, InteriorArtist=0,CoverArtist=0),
by = .(INDEX, ITEM.CODE)]}
# And for artists
DTInteriorArtists = {
DT[, .(Name = unlist(strsplit(as.character(InteriorArtist), ",", fixed = TRUE)),Writer=0, InteriorArtist=1,CoverArtist=0),
by = .(INDEX, ITEM.CODE)]}
# And cover artists
DTCoverArtists = {
DT[, .(Name = unlist(strsplit(as.character(CoverArtist), ",", fixed = TRUE)), Writer=0, InteriorArtist=0,CoverArtist=1),
by = .(INDEX, ITEM.CODE)]}
# Combine into a single object
creatorDF <- rbind(DTWriters, DTInteriorArtists, DTCoverArtists )
# trim whitespace
creatorDF$Name <- trimws(creatorDF$Name, which = "both")
# Now we want to flatten everything down so there's a single row for every creator
# There must be a more efficient way to do this but grouping and summarizing twice does the job ok
# We have to do this because, when a creator has multiple roles inside the same title,
# that title will be counted twice or three times when we calculate their score
creators <- group_by(creatorDF, Name, ITEM.CODE, INDEX ) %>%
summarise(isWriter = sum(Writer), isInteriorArtist=sum(InteriorArtist), isCoverArtist=sum(CoverArtist))
# Then group again.
creators <- group_by(creators, Name ) %>%
summarise(Writer = sum(isWriter), InteriorArtist=sum(isInteriorArtist), CoverArtist=sum(isCoverArtist), sumIndex = sum(INDEX))
# Now set the creators role
creators$role <- ""
creators$role[creators$Writer > 0] <- "Writer"
creators$role[creators$CoverArtist > 0] <- "Cover Artist"
creators$role[creators$InteriorArtist > 0] <- "Interior Artist"
creators$role <- ifelse( (creators$Writer > 0 ) & (( creators$InteriorArtist >0)| (creators$CoverArtist > 0)), "Writer Artist", creators$role)
# restore DF standard behaviour
creators <- as.data.frame(creators)
return(creators)
})
output$publishersLine <- renderPlot({
workingDF <- currentDF()
DrawPublishersLine(workingDF)
})
# Now draw the plots. These will call into the imported code modules to draw a specific chart
output$publishersChart <- renderPlot({
workingDF <- currentDF()
if( input$barType == "line")
{
DrawPublishersLine(workingDF)
}
else
{
DrawPublishersBar(workingDF, input$barType == "polar")
}
})
output$publishersOverall <- renderPlot({
workingDF <- currentDF()
DrawPublishersOverall(workingDF)
})
output$titleSales <- renderPlot({
DrawTitleSalesPlot( currentDF() )
})
output$titleVolumes <- renderPlot({
DrawTitleVolumePlot( currentDF())
})
output$titleFrequencyBreakdown <- renderPlot({
DrawTitleFrequencyByPlot( currentDF(), input$titleFreqBy)
})
output$teamSales <- renderPlot({
DrawCreativeTeamPlots(currentDF(), input$teamDiscipline)
})
output$creatorSales <- renderPlot({
DrawCreatorPlots(currentCreators(), input$creatorDiscipline)
})
})