https://github.com/jhrcook/jhcutils
A bunch of functions that I find myself rewriting in a many scripts.
https://github.com/jhrcook/jhcutils
pkgdown r r-package r-pkg tidygraph utilities utilities-r wrapper
Last synced: 24 days ago
JSON representation
A bunch of functions that I find myself rewriting in a many scripts.
- Host: GitHub
- URL: https://github.com/jhrcook/jhcutils
- Owner: jhrcook
- License: gpl-3.0
- Created: 2019-03-22T11:28:46.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-08-29T13:20:46.000Z (almost 6 years ago)
- Last Synced: 2025-01-13T11:50:09.446Z (over 1 year ago)
- Topics: pkgdown, r, r-package, r-pkg, tidygraph, utilities, utilities-r, wrapper
- Language: R
- Homepage: https://jhrcook.github.io/jhcutils/
- Size: 4.09 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
[](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)
```