-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
204 lines (153 loc) · 6.18 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
set.seed(0)
```
# jhcutils <a href="https://jhrcook.github.io/jhcutils/index.html"> <img src="man/figures/logo.png" align="right" alt="" width="120" /> </a>
[](https://www.gnu.org/licenses/gpl-3.0)
[](https://github.com/jhrcook/jhcutils/actions)
[](https://travis-ci.org/jhrcook/jhcutils)
[](https://ci.appveyor.com/project/jhrcook/jhcutils)
[](https://codecov.io/github/jhrcook/jhcutils?branch=master)
[](https://github.com/jhrcook)
[](https://twitter.com/JoshDoesa)
[](https://joshuacook.netlify.com)
These are a bunch of functions that I find myself declaring and rewriting in a many scripts and analyses.
Full documentation at the ['pkgdown site'](https://jhrcook.github.io/jhcutils/index.html).
## Installation
You can install 'jhcutils' with:
```{r install, eval = FALSE}
devtools::install_github("jhrcook/jhcutils")
```
```{r load_libraries, warning = FALSE, message = FALSE}
library(jhcutils)
library(datasets)
library(tidygraph)
library(dplyr)
set.seed(0)
```
### Additions
If you have any recommended additions, please open an [issue](https://github.com/jhrcook/jhcutils/issues).
---
## General Utilities
`n_unique` - return the number of unique values in a vector.
`unique_na` - return the unique values in a vector, omitting `NA`.
```{r uniquena}
a <- c(1, 2, 3, NA, 3)
unique_na(a)
b <- list(c(1, 2, 3, NA), c(1, 2, NA, 5))
unique_na(b)
unique_na(b, to_unlist = TRUE)
```
`minmax` - set limits on a vector of numeric values.
```{r minmax}
c <- sample(-100:100, 20)
c
minmax(c, -10, 10)
```
`u_pull` - works just like `dplyr::pull()` except only returns unique values. There are also options to return the values sorted and without `NA` using the paramters `sorted` and `na.rm`, respectively.
```{r upull}
str(mtcars$gear)
mtcars %>% u_pull(gear)
```
`vsample` - a safe wrapper for `base::sample()` that always assumes you are passing a vector.
```{r vsample}
# samples from 1:10
sample(10)
# just returns 10
vsample(10)
# samples from 1:5 with replacement
sample(5, 10, replace = TRUE)
# samples from `c(5)` with replacement
vsample(5, 10, replace = TRUE)
```
`str_replace_us` and `str_replace_sp` - replace underscores with spaces, or *vice vera*.
## Tidygraph
```{r myplot, include=FALSE}
library(ggraph)
my_plot_fxn <- function(gr) {
g <- ggraph(gr, layout = "nicely") +
geom_edge_link(color = "grey30", width = 0.5) +
geom_node_point(color = "dodgerblue", size = 7) +
geom_node_text(aes(label = name), size = 4, color = "black") +
theme_void()
return(g)
}
```
`quick_forestfire` and `quick_barabasi`- wrapper around `tidygraph::play_forestfire` and `tidygraph::play_barabasi_albert` except that it will return a tidygraph object with the node attribute `"name"`.
```{r forestfire}
forest_gr <- quick_forestfire(10)
forest_gr
my_plot_fxn(forest_gr) +
labs(title = "Example of a quick Forest Fire graph model")
barabasi_gr <- quick_barabasi(10)
barabasi_gr
my_plot_fxn(barabasi_gr) +
labs(title = "Example of a quick Barabasi-Albert graph")
```
`quick_graph` - randomly selects one of the above random graphs.
`recursive_graph_join` - recursively join a list of tidygraph objects.
```{r recursivegraphjoin}
gr_list <- purrr::map(c(5, 10, 15), quick_forestfire)
gr <- recursive_graph_join(gr_list)
gr
my_plot_fxn(gr) +
labs(title = "Example of joining 3 forest fire graphs")
```
`filter_component_size` - filter the components of a tidygraph object by their individual number of nodes (order).
```{r filtercompsize}
gr <- tidygraph::bind_graphs(quick_forestfire(4, name = LETTERS),
quick_forestfire(6, name = letters))
igraph::count_components(gr)
igraph::count_components(filter_component_size(gr, min_size = 5))
igraph::count_components(filter_component_size(gr, max_size = 5))
```
`get/rm_giant_component` - either return only or everything except the giant component of a graph (i.e. the component with the most number of nodes).
```{r}
gr_large <- quick_forestfire(10, name = LETTERS)
gr_small <- quick_forestfire(5, name = letters)
gr <- tidygraph::bind_graphs(gr_large, gr_small)
gr
get_giant_component(gr)
rm_giant_component(gr)
```
`num_qual_neighbors` - to be used with `tidygraph::map_local_int()` to count the number of neighbors that satisfy a logical expression that is applied to the node attributes of the neighborhood.
```{r num_qual_neighbors}
gr <- quick_barabasi(30)
gr
my_plot_fxn(gr)
# number of neighbors with a "B" in their name
B_gr <- gr %>%
mutate(name_with_B = map_local_int(
.f = num_qual_neighbors,
lgl_filter = rlang::expr(stringr::str_detect(name, "B"))
))
B_gr %N>%
filter(name_with_B > 0) %>%
my_plot_fxn()
```
`get_node_index` - returns the indices of the nodes that pass the expression evaluted in 'dplyr::filter()`.
```{r}
# simple equalities
get_node_index(quick_barabasi(10), name == "B")
get_node_index(quick_barabasi(10), name %in% c("B", "C", "D"))
# can also evaluate functions
get_node_index(quick_barabasi(10), stringr::str_detect(name, "A|B|C"))
```
## Pacakge Utilities
`document_df` - prints the framework for documenting a data frame object.
```{r documentdf}
dat <- tibble::tibble(x = c(LETTERS[1:5]),
y = c(1:5),
z = list(rep(list(1:3), 5)))
dat
document_df(dat)
```