-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadspend.3.3.R
155 lines (121 loc) · 5.11 KB
/
adspend.3.3.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
## Global Adex by medium
version <- 3.3
## User variables
meta <- list(title = "Advertising Expenditure by Medium, Region",
type = "time series, line",
format = "US$ millions, current prices",
note = "Net of discounts. Includes agency commission and excludes production costs. WARC uses variable exchange rates for each period.",
source = "WARC",
x.axis = "Year",
y.axis = "Ad Spend, US$ millons",
legend = "Medium",
date.format = "%Y")
## Dependencies
scripts <- grep(".R", list.files(paste0(getwd(), "/scripts/")), value = TRUE)
if (length(scripts) > 0) {
for (func in scripts)
source(paste0(getwd(), "/scripts/", func))
}
## Load WARC styles
warcStyle()
packages <- c("shiny", "plotly", "XLConnect", "shinythemes", "ggplot2", "reshape2", "abind", "forcats", "showtext", "webshot")
reqPackages(packages)
Sys.getenv()["APPDATA"]
if (!file.exists(paste0(Sys.getenv()["APPDATA"], "\\PhantomJS\\phantomjs.exe")))
webshot::install_phantomjs()
## Paths
path <- paste0(getwd(), "/data.xlsx")
## Read Data
df <- NULL
file <- loadWorkbook(path)
setMissingValue(file, value = "")
for (sht in 1:length(getSheets(file))) {
data <- readWorksheet(file, sht, header = TRUE, startRow = 6, endRow = 16, dateTimeFormat = meta$date.format, forceConversion = TRUE, colTypes = rep("numeric", 10))
## Formatting
names(data)[1] <- "Year"
## Billions, to one d.p.
data[, 2:10] <- apply(data[, 2:10], 1:2, function(x) {
round(x / 1000, 1)}
)
## Sheet into 3D dataframe
df <- abind(df, data, along = 3)
}
dimnames(df)[[3]] <- getSheets(file)
# Define UI
ui <- fixedPage(includeCSS("styles.css"),
# Application title
headerPanel(paste(meta$title, version)),
# Sidebar with Date Range
sidebarPanel(width = 2,
sliderInput("years",
"Select Date Range",
min = df[1,"Year", "Global"],
max = df[dim(df)[1],"Year", "Global"],
value = c(df[1,"Year", "Global"], df[dim(df)[1],"Year", "Global"]),
sep = ""),
checkboxGroupInput("medium",
"Select Mediums",
choices = dimnames(df)[[2]][3:10],
selected = dimnames(df)[[2]][3:10])
),
# Plot Panel
mainPanel(uiOutput("tabs"), width = 10),
downloadButton("pngExport")
)
# Define server logic
server <- function(input, output, session) {
# # close the R session when Chrome closes
# session$onSessionEnded(function() {
# stopApp()
# q("no")
# })
## Generate Tabset
output$tabs <- renderUI({
tabset <- list()
for (i in 1:dim(df)[3]){
## Generate tab
tab <- tabPanel(dimnames(df)[[3]][i],
## Generate plot
renderPlotly({
## Select data
n.tab <- req(input$tabs)
x.min <- match(as.character(input$years[1]), df[,"Year", n.tab])
x.max <- match(as.character(input$years[2]), df[,"Year", n.tab])
x.range <- df[x.min:x.max, "Year" , n.tab]
data <- data.frame(df[, , n.tab])
data[, 2:10] <- apply(data[, 2:10], 2, as.numeric)
data.m <- melt(data[match(x.range, data$Year), c("Year", input$medium)], id.vars = "Year")
## Plot
assign(paste0(n.tab, "Plot"),
ggplotly(
ggplot(data.m, aes(Year, value, fill = forcats::fct_rev(variable))) +
geom_bar(stat = "identity", width = .7) +
labs(x = meta$x.axis, y = meta$y.axis) +
## WARC Styling
scale_fill_manual(values = rev(unlist(styles$col, use.names = FALSE)),
name = meta$legend) +
theme(text=element_text(family = "Aktiv Grotesk Medium",
colour = styles$col$main$col1))
), pos = .GlobalEnv)
}),
hr(),br(),
renderUI({
req(input$tabs)
includeHTML(paste0(getwd(), "/copy/", input$tabs,".html"))
})
)
## Append tab to tabset
tabset <- append(tabset, list(tab))
}
do.call(tabsetPanel, args = c(tabset, id = "tabs"))
})
output$pngExport <- downloadHandler(
filename = paste(input$tabs, '.png', sep=''),
content <- function(file) {
export(p = get(paste0(input$tabs, "Plot")), file = paste0(input$tabs, "Plot.png"))
file.copy(paste0(input$tabs, "Plot.png"), file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)