-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
141 lines (125 loc) · 5.75 KB
/
server.R
File metadata and controls
141 lines (125 loc) · 5.75 KB
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
library(shiny,quietly = T)
library(shinyjs, quietly = T)
library(dplyr,quietly = T)
library(ggplot2,quietly = T)
library(data.table,quietly = T)
source("helper.R")
load("./data/map.RData")
shinyServer(function(input, output) {
withProgress(load("./data/CRANlog_cleaned_month.RData"),value=99,message="Loading 20,086,138 rows of data ... This takes 11 seconds! Please wait while it's loading.")
options(warn = -1)
react.plot <- eventReactive(input$go,{
shiny::validate(
need(length(input$package)>0, "Select at least one package.")
)
dat.ts <- aggregate(times ~ package + month, data=subset(dat, package %in% input$package), sum)
switch(input$plot_type,
"Downloads vs Time (monthly)" = {
if (input$smooth){
ggplot(dat.ts, aes(month, times, color = package)) + stat_smooth(se=FALSE, span=input$smooth.span) +
geom_line() + xlab("Date") + scale_y_continuous(name="Number of downloads", labels = scales::comma)
} else {
ggplot(dat.ts, aes(month, times, color = package)) +
geom_line() + xlab("Date") + scale_y_continuous(name="Number of downloads", labels = scales::comma)
}},
"Downloads vs Time (cumulative)" = {
d = dat.ts %>%
group_by(package) %>%
transmute(count=cumsum(times), month=month)
ggplot(d, aes(month, count, color = package)) + geom_line() +
xlab("Date") + scale_y_continuous(name="Number of downloads", labels = scales::comma)},
"Map (cumulative)" = {
shiny::validate(
need(length(input$package)==1, "Select one package for Map (cumulative) plot.")
)
withProgress(message = "Working really hard to plot ... ", value = 99,{
dat.p <- subset(dat,package %in% input$package)
ma <- plyr::join(ma, aggregate(times ~ country, data = dat.p, sum), by ="country")
if (!complete.cases(ma)){ ma[!complete.cases(ma),]$times <- 0 }
ggplot() + geom_polygon(data = ma, aes(x = long, y = lat, group = group, fill = log10(times)),
colour = "black", size = 0.3) +
ggtitle(paste("Map of the Cumulative Downloads of",input$package)) +
annotate("text",x=-100,y=-75,label=paste("Total download since",months(dat.p$month[1]),year(dat.p$month[1]),
"is",sum(dat.p$times))) + coord_fixed(ratio = 1.4)
})
},
"Map (dominance)" = {
shiny::validate(
need(length(input$package)==2, "Select two packages for Map (dominance) plot.")
)
withProgress(message = "Plotting really hard ... ", value = 99, {
dat.p <- subset(dat,package %in% input$package)
dat.p1 <- subset(dat.p,package==input$package[1] & month>=input$times[1] & month<=input$times[2])
dat.p2 <- subset(dat.p,package==input$package[2] & month>=input$times[1] & month<=input$times[2])
colnames(dat.p1)[5] <- "times1"
colnames(dat.p2)[5] <- "times2"
domination <- plyr::join(aggregate(times1 ~ country, data = dat.p1, sum),
aggregate(times2 ~ country, data = dat.p2, sum), by ="country", type = "full")
domination[is.na(domination)] <- 0
for (i in 1:nrow(domination)){
if(domination$times1[i]+domination$times2[i]==0){
domination$dominance[i]<-"None"
} else {if (domination$times1[i]>=domination$times2[i]){
domination$dominance[i]<-paste(input$package[1])
} else {
domination$dominance[i]<-paste(input$package[2])
}}
}
ma <- plyr::join(ma, domination, by ="country")
if (!complete.cases(ma)){ ma[!complete.cases(ma),]$times <- 0 }
ggplot() +
geom_polygon(data = ma, aes(x = long, y = lat, group = group, fill = dominance),
colour = "black", size = 0.3) +
ggtitle(paste("Dominance Map of",input$package[1],"and",input$package[2])) + coord_fixed(ratio = 1.4)
}
)
})
})
output$plot1 <- renderPlot({
react.plot()
})
output$timeslide <- renderUI({
if(input$plot_type=="Map (dominance)"){
range.time <- range(subset(dat, package %in% input$package)$month)
sliderInput("times","Time Selection",min=range.time[1],max=range.time[2],
value=range.time,timeFormat="%b %Y")
}
})
output$smooth.spanning <- renderUI({
if (is.null(input$smooth)) {
} else if(input$smooth & input$plot_type=="Downloads vs Time (monthly)"){
sliderInput("smooth.span","Smoothness Level",min=0.2,max=1.0,value=0.5)
}
})
output$smoothing <- renderUI({
if (input$plot_type=="Downloads vs Time (monthly)")
checkboxInput("smooth", "With smoothing?", value = FALSE)
})
react.top5 <- eventReactive(input$go,{
if (input$plot_type=="Map (cumulative)"){
shiny::validate(
need(length(input$package)==1, "")
)
dat.p <- subset(dat,package %in% input$package)
dat.p$country.name <- countrycode::countrycode(dat.p$country.name, "country.name","country.name")
dat.country <- aggregate(times ~ country.name, data=dat.p, FUN = sum)
dat.country[order(dat.country$times,decreasing = T),][1:10,]
} else {NULL}
})
output$top5 <- renderTable({
react.top5()
}, caption = "Top ten countries by download times.")
output$download_data <- renderUI({
downloadLink('downloaddata', "Download Selected Data")
})
output$downloaddata <- downloadHandler(
# specify the file name
filename = function() {
paste(input$package, 'csv', sep='.')
},
#open the device, create the plot, close the device
content = function(file) {
write.csv(dat.p,file)
}
)
})