-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
182 lines (144 loc) · 5.38 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
message = FALSE,
warning = FALSE
)
library(functiondepends)
```
# functiondepends <img src="man/figures/logo.png" align="right" width="120" />
<!-- badges: start -->
[![R build status](https://github.com/jakubsob/functiondepends/workflows/R-CMD-check/badge.svg)](https://github.com/jakubsob/functiondepends/actions)
[![license](https://img.shields.io/badge/license-mit-lightgrey.svg)](https://choosealicense.com/)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/functiondepends)](https://cran.r-project.org/package=functiondepends)
[![CRAN_latest_release_date](https://www.r-pkg.org/badges/last-release/functiondepends)](https://cran.r-project.org/package=functiondepends)
[![CRAN status](https://cranlogs.r-pkg.org/badges/grand-total/functiondepends)](https://CRAN.R-project.org/package=functiondepends)
<!-- badges: end -->
The goal of functiondepends is to allow for tidy exploration of unstructured codebase without evaluation of code.
## Installation
One can install `functiondepends` from CRAN:
```{r, eval = FALSE}
install.packages("functiondepends")
```
or development version from GitHub:
```{r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("jakubsob/functiondepends")
```
## Examples
```{r, include = FALSE}
envir <- functiondepends:::envir
functions <- functiondepends:::functions
```
```{r, eval = FALSE}
library(functiondepends)
# Create environment for loaded functions
envir <- new.env()
# Search recursively current directory
functions <- find_functions(".", envir = envir, recursive = TRUE)
```
```{r}
functions
```
Search for dependencies of function `find_functions` within parsed functions:
```{r}
dependency <- find_dependencies("find_functions", envir = envir, in_envir = TRUE)
dependency
```
Note that `SourceNamespace` column has value `user-defined` as the functions are searched within source of the package.
Search for all dependencies of `find_functions` function:
```{r functions_in_path, fig.height=4}
library(ggplot2)
library(dplyr)
dependency <- find_dependencies("find_functions", envir = envir, in_envir = FALSE)
dependency %>%
slice_max(SourceRep, n = 10) %>%
mutate(Source = reorder(Source, SourceRep)) %>%
ggplot(aes(x = Source, y = SourceRep, fill = SourceNamespace)) +
geom_col() +
coord_flip() +
labs(caption = "Top 10 most repeated calls in 'find_functions'.")
```
Note that name `df` is often used to store object of type `data.frame`. `df` is also a name of F distribution density function from `stats` package. If you suspect that given function ought not to use a specific package, see the source code of function to check the context. To do so, one can execute `find_dependencies` function with `add_info` argument set to `TRUE`.
```{r}
library(tidyr)
dependency <- find_dependencies("find_functions", envir = envir, in_envir = FALSE, add_info = TRUE)
dependency %>%
filter(SourceNamespace == "stats") %>%
select(Source, SourcePosition, SourceContext) %>%
unnest(c(SourcePosition, SourceContext))
```
One can see that indeed `df` is not a call to function `stats::df`.
```{r target_degree, fig.height=4}
dependency <- find_dependencies(unique(functions$Function), envir = envir, in_envir = FALSE)
dependency %>%
distinct(Target, TargetInDegree) %>%
mutate(Target = reorder(Target, TargetInDegree)) %>%
ggplot(aes(x = Target, y = TargetInDegree)) +
geom_col() +
coord_flip() +
labs(caption = "Functions with most function calls.")
```
```{r namespace_count, fig.height=4}
dependency <- find_dependencies(unique(functions$Function), envir = envir, in_envir = FALSE)
dependency %>%
group_by(SourceNamespace) %>%
tally(name = "Count") %>%
slice_max(Count, n = 10) %>%
mutate(SourceNamespace = reorder(SourceNamespace, Count)) %>%
ggplot(aes(x = SourceNamespace, y = Count)) +
geom_col() +
coord_flip() +
labs(caption = "Top 10 used namespaces.")
```
See which user-defined functions depend most on other user-defined functions within searched codebase.
```{r}
dependency <- find_dependencies(unique(functions$Function), envir = envir, in_envir = TRUE)
dependency %>%
distinct(Target, TargetInDegree) %>%
arrange(-TargetInDegree)
```
```{r network_env, fig.height=4}
library(igraph)
edges <- dependency %>%
select(Source, Target) %>%
na.omit()
vertices <- unique(c(dependency$Source, dependency$Target))
vertices <- vertices[!is.na(vertices)]
g <- graph_from_data_frame(d = edges, vertices = vertices)
deg <- degree(g, mode = "in")
V(g)$size <- deg * 10 + 5
V(g)$label.cex <- (degree(g, mode = "in", normalized = TRUE) + 1)
plot(
g,
vertex.color = "grey",
edge.color = "grey",
edge.arrow.size = .4,
main = "Functions dependency graph"
)
```
```{r network, fig.height=4}
dependency <- find_dependencies(unique(functions$Function), envir = envir, in_envir = FALSE)
edges <- dependency %>%
select(Source, Target) %>%
na.omit()
vertices <- unique(c(edges$Source, edges$Target))
g <- graph_from_data_frame(edges)
deg <- degree(g, mode = "in")
V(g)$size <- deg
V(g)$label.cex <- (degree(g, mode = "in", normalized = TRUE) + 1) / 1.8
plot(
g,
vertex.color = "grey",
edge.color = "grey",
edge.arrow.size = .4,
main = "Full functions dependency graph"
)
```