forked from dgrtwo/data-screencasts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cran-code.Rmd
241 lines (202 loc) · 5.7 KB
/
cran-code.Rmd
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
theme_set(theme_light())
tuesdata <- tidytuesdayR::tt_load("2019-11-12")
cran_code <- tuesdata$loc_cran_packages
```
```{r}
View(cran_code)
```
What the most common programming languages in CRAN packages?
```{r}
cran_code %>%
count(language, sort = TRUE)
```
```{r}
by_language <- cran_code %>%
group_by(language) %>%
summarize(packages = n(),
code = sum(code),
comments = sum(comment),
files = sum(file),
lines_per_package = code / packages,
files_per_package = files / packages,
comment_code_ratio = comments / code) %>%
arrange(desc(packages))
```
```{r}
by_language %>%
head(20) %>%
mutate(language = fct_reorder(language, packages)) %>%
ggplot(aes(language, packages)) +
geom_col() +
coord_flip() +
labs(x = "",
y = "# of packages that have code from this language")
library(tidytext)
by_language %>%
gather(metric, value, packages, code, files) %>%
group_by(metric) %>%
top_n(8, value) %>%
ungroup() %>%
mutate(language = reorder_within(language, value, metric),
metric = str_to_title(metric)) %>%
ggplot(aes(language, value)) +
geom_col() +
coord_flip() +
scale_x_reordered() +
scale_y_continuous(labels = scales::comma) +
facet_wrap(~ metric, scales = "free", ncol = 1) +
labs(x = "Value (# of lines of code, files, or packages)")
```
How much are languages commented?
```{r}
by_language %>%
filter(packages >= 20) %>%
ggplot(aes(packages, comment_code_ratio)) +
geom_point() +
geom_text(aes(label = language), check_overlap = TRUE, vjust = 1, hjust = 1) +
scale_x_log10() +
expand_limits(x = 10) +
labs(x = "# of packages language is used in",
y = "Comment/Code ratio")
```
```{r}
by_language %>%
filter(packages >= 20) %>%
ggplot(aes(packages, lines_per_package)) +
geom_point() +
geom_text(aes(label = language), check_overlap = TRUE, vjust = 1, hjust = 1) +
scale_x_log10() +
expand_limits(x = 10) +
labs(x = "# of packages language is used in",
y = "Lines per package")
```
How much R code is there in each package?
```{r}
cran_code %>%
filter(language == "R") %>%
ggplot(aes(code)) +
geom_histogram() +
scale_x_log10(labels = scales::comma)
```
```{r}
cran_code %>%
filter(language == "R") %>%
arrange(desc(code))
```
Let's look just at the tidyverse packages
```{r}
packages <- tidyverse_packages() %>%
str_extract("[a-z\\d]+")
cran_code %>%
filter(pkg_name %in% packages) %>%
mutate(pkg_name = fct_reorder(pkg_name, code, sum),
language = fct_lump(language, 6),
language = fct_reorder(language, code, sum)) %>%
ggplot(aes(pkg_name, code, fill = language)) +
geom_col() +
guides(fill = guide_legend(reverse = TRUE)) +
coord_flip() +
labs(title = "How much code does each tidyverse package have?",
x = "",
y = "# of lines of code",
fill = "Language")
```
```{r}
cran_code %>%
filter(pkg_name %in% packages) %>%
filter(language == "R") %>%
mutate(comment_code_ratio = comment / code) %>%
arrange(desc(comment_code_ratio)) %>%
View()
```
```{r}
cran_code %>%
filter(language == "R", code >= 100) %>%
mutate(tidyverse = ifelse(pkg_name %in% packages, "Tidyverse", "Other")) %>%
ggplot(aes(code / comment)) +
geom_histogram() +
scale_x_log10(labels = scales::number_format(accuracy = .1)) +
facet_wrap(~ tidyverse, ncol = 1, scales = "free_y") +
labs(x = "Code to comment ratio")
```
```{r}
cran_code %>%
filter(code >= 100, language == "R", comment > 0) %>%
mutate(code_comment_ratio = code / comment) %>%
arrange(desc(code_comment_ratio))
```
```{r}
pkgs <- available.packages()
head(pkgs)
# Take package downloads from yesterday
package_downloads <- read_csv("http://cran-logs.rstudio.com/2019/2019-12-19.csv.gz")
downloads_by_package <- package_downloads %>%
distinct(package, ip_id) %>%
count(pkg_name = package, sort = TRUE, name = "downloads")
```
```{r}
cran_code %>%
filter(language == "R") %>%
inner_join(downloads_by_package, by = "pkg_name") %>%
arrange(desc(downloads)) %>%
filter(downloads >= 10) %>%
ggplot(aes(downloads, code)) +
geom_point() +
geom_smooth(method = "lm") +
scale_x_log10() +
scale_y_log10()
cran_code %>%
filter(language == "R") %>%
inner_join(downloads_by_package, by = "pkg_name") %>%
arrange(desc(downloads)) %>%
filter(downloads >= 10, code >= 10) %>%
ggplot(aes(downloads, code / comment)) +
geom_point() +
geom_smooth(method = "lm") +
scale_x_log10() +
scale_y_log10()
```
```{r}
package_metadata <- available.packages() %>%
as_tibble() %>%
janitor::clean_names() %>%
select(-version, -file)
cran_code %>%
filter(language == "R") %>%
inner_join(package_metadata, by = c(pkg_name = "package")) %>%
mutate(license = fct_lump(license, 10),
license = fct_reorder(license, code)) %>%
ggplot(aes(license, code)) +
geom_boxplot() +
coord_flip() +
scale_y_log10()
n_import <- package_metadata %>%
select(package, imports) %>%
separate_rows(imports, sep = ",") %>%
extract(imports, "import", "([A-Za-z\\d\\.]+)") %>%
filter(!is.na(import)) %>%
count(import, sort = TRUE, name = "n_reverse_import")
```
```{r}
cran_code %>%
filter(language == "R") %>%
left_join(n_import, by = c(pkg_name = "import")) %>%
replace_na(list(n_reverse_import = 0)) %>%
filter(n_reverse_import >= 10) %>%
ggplot(aes(n_reverse_import, code)) +
geom_point() +
geom_text(aes(label = pkg_name), vjust = 1, hjust = 1, check_overlap = TRUE) +
scale_x_log10() +
scale_y_log10() +
labs(x = "Number of packages that IMPORT this",
y = "Lines of R code in package")
```