-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
196 lines (177 loc) · 6.44 KB
/
app.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
library(cranlogs)
library(shiny)
library(ggplot2)
library(lubridate)
library(dplyr)
library(patchwork)
library(tidyr)
library(purrr)
library(tools)
ui <- fluidPage(
theme = bslib::bs_theme(version = 4, bootswatch = "darkly"),
titlePanel("CRAN Package Downloads"),
sidebarLayout(
sidebarPanel(
selectizeInput("package_name", "Select CRAN Packages:",
choices = NULL,
multiple = TRUE,
options = list(maxItems = 10)),
radioButtons("time_unit", "Time Unit:",
choices = c("Daily" = "daily",
"Weekly" = "weekly",
"Monthly" = "monthly"),
selected = "daily"),
dateInput("from_date", "From Date:",
value = Sys.Date() - 30,
max = Sys.Date()),
dateInput("to_date", "To Date:",
value = Sys.Date(),
max = Sys.Date()),
actionButton("submit", "Get Download Stats",
class = "btn-primary"),
h3("Summary"),
tags$div(
style = "height: 350px; overflow-y: scroll;",
verbatimTextOutput("download_summary")
)
),
mainPanel(
plotOutput("download_plot", height = "400px"),
br(),
plotOutput("total_downloads", height = "400px")
)
)
)
server <- function(input, output, session) {
# Get list of all CRAN packages
all_packages <- as.data.frame(available.packages(repos = "https://cloud.r-project.org"))
# Initialize selectize input with all CRAN packages, default to ggplot2 and dplyr
updateSelectizeInput(session, "package_name",
choices = sort(all_packages$Package),
selected = c("ggplot2", "dplyr"),
server = TRUE,
options = list(
maxItems = 10,
placeholder = 'Select packages'
))
# Reactive function to get download stats
get_download_stats <- eventReactive(input$submit, {
req(input$package_name)
from_date <- input$from_date
to_date <- input$to_date
withProgress(message = 'Fetching download stats...', {
# Get downloads for all selected packages
all_downloads <- lapply(input$package_name, function(pkg) {
incProgress(1/length(input$package_name))
downloads <- try(cran_downloads(pkg, from = from_date, to = to_date))
if(inherits(downloads, "try-error")) {
return(NULL)
}
downloads$package <- pkg
return(downloads)
})
})
# Remove NULL entries and combine all downloads into one dataframe
downloads <- bind_rows(all_downloads[!sapply(all_downloads, is.null)])
if(nrow(downloads) == 0) {
return(NULL)
}
downloads %>%
mutate(
weekly = floor_date(date, "week"),
monthly = floor_date(date, "month")
)
})
# Plot the downloads based on selected time unit
output$download_plot <- renderPlot({
req(get_download_stats())
data <- get_download_stats()
time_group <- switch(input$time_unit,
"daily" = "date",
"weekly" = "weekly",
"monthly" = "monthly")
grouped_data <- data %>%
group_by(package, !!sym(time_group)) %>%
summarise(count = sum(count), .groups = "drop")
ggplot(grouped_data, aes(x = !!sym(time_group), y = count, color = package)) +
geom_line(linewidth = 1) +
scale_color_brewer(palette = "Set1") +
labs(
x = NULL,
y = "Downloads",
title = paste(stringr::str_to_title(input$time_unit), "Downloads")
) +
theme_dark(base_size = 15) +
theme(
text = element_text(color = "white"),
plot.title = element_text(hjust = 0.5),
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
panel.grid = element_line(color = "grey30"),
legend.background = element_rect(fill = "black"),
legend.text = element_text(color = "white"),
legend.title = element_text(color = "white"),
legend.key = element_rect(fill = "black"),
legend.position = "bottom"
)
})
# Plot total downloads since CRAN release
output$total_downloads <- renderPlot({
req(get_download_stats())
data <- get_download_stats()
cumulative_data <- data %>%
group_by(package) %>%
arrange(date) %>%
mutate(cumulative = cumsum(count))
ggplot(cumulative_data, aes(x = date, y = cumulative, color = package)) +
geom_line(linewidth = 1) +
scale_color_brewer(palette = "Set1") +
labs(
x = NULL,
y = "Downloads",
title = "Total Downloads Since CRAN Release"
) +
theme_dark(base_size = 15) +
theme(
text = element_text(color = "white"),
plot.title = element_text(hjust = 0.5),
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
panel.grid = element_line(color = "grey30"),
legend.background = element_rect(fill = "black"),
legend.text = element_text(color = "white"),
legend.title = element_text(color = "white"),
legend.key = element_rect(fill = "black"),
legend.position = "none"
)
})
# Display the download statistics summary
output$download_summary <- renderText({
req(get_download_stats())
data <- get_download_stats()
summary_stats <- data %>%
group_by(package) %>%
summarise(
total_downloads = format(sum(count), big.mark = ","),
avg_downloads_per_day = format(round(mean(count), 0), big.mark = ","),
avg_downloads_per_week = format(round(sum(count) / n_distinct(weekly), 0), big.mark = ","),
avg_downloads_per_month = format(round(sum(count) / n_distinct(monthly), 0), big.mark = ","),
.groups = "drop"
)
paste(
sapply(1:nrow(summary_stats), function(i) {
stats <- summary_stats[i, ]
sprintf(
"Package: %s\nTotal Downloads: %s\nAverage Downloads per Day: %s\nAverage Downloads per Week: %s\nAverage Downloads per Month: %s\n\n",
stats$package,
stats$total_downloads,
stats$avg_downloads_per_day,
stats$avg_downloads_per_week,
stats$avg_downloads_per_month
)
}),
collapse = ""
)
})
}
shinyApp(ui, server)