forked from rstudio/shiny-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
99 lines (81 loc) · 2.7 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
function(input, output, session) {
# pkgStream is a reactive expression that represents a stream of
# new package download data; up to once a second it may return a
# data frame of new downloads since the last update.
pkgStream <- packageStream(session)
# Max age of data (5 minutes)
maxAgeSecs <- 60 * 5
# pkgData is a reactive expression that accumulates previous
# values of pkgStream, discarding any that are older than
# maxAgeSecs.
pkgData <- packageData(pkgStream, maxAgeSecs)
# dlCount is a reactive expression that keeps track of the total
# number of rows that have ever appeared through pkgStream.
dlCount <- downloadCount(pkgStream)
# usrCount is a reactive expression that keeps an approximate
# count of all of the unique users that have been seen since the
# app started.
usrCount <- userCount(pkgStream)
# Record the time that the session started.
startTime <- as.numeric(Sys.time())
output$rate <- renderValueBox({
# The downloadRate is the number of rows in pkgData since
# either startTime or maxAgeSecs ago, whichever is later.
elapsed <- as.numeric(Sys.time()) - startTime
downloadRate <- nrow(pkgData()) / min(maxAgeSecs, elapsed)
valueBox(
value = formatC(downloadRate, digits = 1, format = "f"),
subtitle = "Downloads per sec (last 5 min)",
icon = icon("area-chart"),
color = if (downloadRate >= input$rateThreshold) "yellow" else "aqua"
)
})
output$count <- renderValueBox({
valueBox(
value = dlCount(),
subtitle = "Total downloads",
icon = icon("download")
)
})
output$users <- renderValueBox({
valueBox(
usrCount(),
"Unique users",
icon = icon("users")
)
})
output$packagePlot <- renderBubbles({
if (nrow(pkgData()) == 0)
return()
order <- unique(pkgData()$package)
df <- pkgData() %>%
group_by(package) %>%
tally() %>%
arrange(desc(n), tolower(package)) %>%
# Just show the top 60, otherwise it gets hard to see
head(60)
bubbles(df$n, df$package, key = df$package)
})
output$packageTable <- renderTable({
pkgData() %>%
group_by(package) %>%
tally() %>%
arrange(desc(n), tolower(package)) %>%
mutate(percentage = n / nrow(pkgData()) * 100) %>%
select("Package name" = package, "% of downloads" = percentage) %>%
as.data.frame() %>%
head(15)
}, digits = 1)
output$downloadCsv <- downloadHandler(
filename = "cranlog.csv",
content = function(file) {
write.csv(pkgData(), file)
},
contentType = "text/csv"
)
output$rawtable <- renderPrint({
orig <- options(width = 1000)
print(tail(pkgData(), input$maxrows))
options(orig)
})
}