knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

set.seed(0)

jhcutils

License: GPL v3 R build status Travis build status AppVeyor build status Coverage status
jhc github jhc twitter jhc website

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'.

Installation

You can install 'jhcutils' with:

devtools::install_github("jhrcook/jhcutils")
library(jhcutils)
library(datasets)
library(tidygraph)
library(dplyr)
set.seed(0)

Additions

If you have any recommended additions, please open an issue.


General Utilities

n_unique - return the number of unique values in a vector.

unique_na - return the unique values in a vector, omitting NA.

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.

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.

str(mtcars$gear)
mtcars %>% u_pull(gear)

vsample - a safe wrapper for base::sample() that always assumes you are passing a vector.

# 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

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".

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.

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).

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).

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.

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()`.

# 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.

dat <- tibble::tibble(x = c(LETTERS[1:5]),
                      y = c(1:5),
                      z = list(rep(list(1:3), 5)))
dat
document_df(dat)


jhrcook/jhcutils documentation built on Sept. 2, 2020, 7:16 a.m.