Skip to content

Commit 4285d64

Browse files
added utilities from northstar
1 parent 9de22ef commit 4285d64

23 files changed

+487
-13
lines changed

DESCRIPTION

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ Imports:
1818
stringr,
1919
styler,
2020
tidyr,
21-
vctrs
21+
vctrs,
22+
rlang,
23+
collapse
2224
Suggests:
2325
prettycode,
26+
gt,
27+
fontawesome,
2428
roxyglobals,
2529
testthat (>= 3.0.0)
2630
Config/roxyglobals/filename: generated-globals.R

NAMESPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export("%nin%")
4+
export("%nn%")
35
export(clean_number)
46
export(count_missing)
57
export(count_prop)
8+
export(display_long)
9+
export(gh_raw)
10+
export(gt_marks)
11+
export(invert_named)
612
export(is_directory)
713
export(is_readable)
814
export(months_regex)
15+
export(na_blank)
916
export(rename_seq)
17+
export(search_in)
1018
export(single_line_string)
19+
export(srchcol)

R/display_long.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#' Pivot data frame to long format for easy printing
2+
#'
3+
#' @param df `<data.frame>` or `<tibble>` to pivot long
4+
#'
5+
#' @param cols `<character>` vector of columns to pivot long, default is [dplyr::everything()]
6+
#'
7+
#' @returns a `<data.frame>` or `<tibble>` in long format
8+
#'
9+
#' @examples
10+
#' x <- dplyr::tibble(
11+
#' a = 1:10,
12+
#' b = letters[1:10],
13+
#' c = 11:20,
14+
#' d = LETTERS[1:10],
15+
#' e = 21:30)
16+
#'
17+
#' display_long(x)
18+
#'
19+
#' @autoglobal
20+
#'
21+
#' @export
22+
display_long <- function(df, cols = dplyr::everything()) {
23+
24+
df |> dplyr::mutate(
25+
dplyr::across(
26+
dplyr::everything(), as.character)) |>
27+
tidyr::pivot_longer({{ cols }})
28+
}

R/gh_raw.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#' Return GitHub raw url
2+
#'
3+
#' @param x `<chr>` string
4+
#'
5+
#' @returns `<chr>` GitHub raw url
6+
#'
7+
#' @examples
8+
#' gh_raw("andrewallenbruce/example/main/inst/pins/")
9+
#'
10+
#' @autoglobal
11+
#'
12+
#' @export
13+
gh_raw <- function(x) {
14+
paste0(
15+
"https://raw.githubusercontent.com/",
16+
x
17+
)
18+
}

R/gt_marks.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#' gt Marks
2+
#'
3+
#' @param gt_tbl description
4+
#'
5+
#' @param cols description
6+
#'
7+
#' @returns description
8+
#'
9+
#' @export
10+
#'
11+
#' @keywords internal
12+
#'
13+
#' @autoglobal
14+
gt_marks <- function(gt_tbl, cols) {
15+
16+
gt_tbl |>
17+
gt::text_case_when(
18+
x == TRUE ~ gt::html(
19+
fontawesome::fa("check",
20+
prefer_type = "solid",
21+
fill = "red")),
22+
x == FALSE ~ gt::html(
23+
fontawesome::fa("xmark",
24+
prefer_type = "solid",
25+
fill = "white")),
26+
.default = NA,
27+
.locations = gt::cells_body(
28+
columns = {{ cols }}))
29+
}

R/infix.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#' Infix operator for `if (!is.null(x)) y else x` statements
2+
#'
3+
#' @param x,y vectors
4+
#'
5+
#' @examples
6+
#' NULL %nn% 123456L
7+
#'
8+
#' "abc" %nn% 123456L
9+
#'
10+
#' @returns `y` if `x` is not `NULL`, else `x`
11+
#'
12+
#' @autoglobal
13+
#'
14+
#' @export
15+
`%nn%` <- function(x, y) {
16+
if (!is.null(x))
17+
y
18+
else
19+
x
20+
}
21+
22+
#' Infix operator for `not in` statements
23+
#'
24+
#' @param x vector
25+
#'
26+
#' @param table vector
27+
#'
28+
#' @returns logical vector
29+
#'
30+
#' @autoglobal
31+
#'
32+
#' @export
33+
`%nin%` <- function(x, table) {
34+
match(x, table, nomatch = 0L) == 0L
35+
}

R/invert_named.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#' Invert a named vector
2+
#'
3+
#' @param x A named vector
4+
#'
5+
#' @returns A named vector with names and values inverted
6+
#'
7+
#' @examples
8+
#' invert_named(x = c(name = "element"))
9+
#'
10+
#' invert_named(x = c(element = "name"))
11+
#'
12+
#' @autoglobal
13+
#'
14+
#' @export
15+
invert_named <- function(x) {
16+
17+
stopifnot(
18+
"Input must be a named vector" = !is.null(names(x)))
19+
20+
rlang::set_names(names(x), unname(x))
21+
}

R/na_blank.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#' Convert various character values to `NA`
2+
#'
3+
#' @param x `<chr>` vector
4+
#'
5+
#' @returns `<chr>` vector with `NA` values
6+
#'
7+
#' @examples
8+
#' na_blank(x = c(" ", "*", "--", "N/A", ""))
9+
#'
10+
#' @autoglobal
11+
#'
12+
#' @export
13+
na_blank <- function(x) {
14+
15+
y <- c("", " ", "*", "--", "N/A")
16+
17+
x <- dplyr::na_if(x, y)
18+
19+
}

R/rename_seq.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#' style = TRUE)
2828
#'
2929
#' @autoglobal
30+
#'
3031
#' @export
3132
rename_seq <- function(n,
3233
new,

R/search_in.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#' Search in data frame
2+
#'
3+
#' @param df A `<data.frame>` or `<tibble>`
4+
#'
5+
#' @param dfcol A `<character>` or `<symbol>` specifying the column to search in
6+
#'
7+
#' @param search A `<character>` or `<symbol>` specifying the search term
8+
#'
9+
#' @returns A `<data.frame>` or `<tibble>`
10+
#'
11+
#' @examples
12+
#' x <- dplyr::tibble(y = 1:10, z = letters[1:10])
13+
#'
14+
#' search_in(df = x, dfcol = x$z, search = c("a", "j"))
15+
#'
16+
#' @autoglobal
17+
#'
18+
#' @export
19+
search_in <- function(df, dfcol, search) {
20+
vctrs::vec_slice(
21+
df,
22+
vctrs::vec_in(
23+
dfcol,
24+
collapse::funique(
25+
search
26+
)
27+
)
28+
)
29+
}

R/srchcol.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#' Search a data frame column by string
2+
#'
3+
#' @param df `<data.frame>` or `<tibble>`
4+
#'
5+
#' @param col column name as string or unquoted
6+
#'
7+
#' @param search string to search for, can be a regular expression, e.g. `'^[A-Z]'`
8+
#'
9+
#' @param ignore ignore case, default is `TRUE`
10+
#'
11+
#' @param ... additional arguments
12+
#'
13+
#' @returns `<data.frame>` or `<tibble>`
14+
#'
15+
#' @examples
16+
#' x <- dplyr::tibble(y = 1:10, z = letters[1:10])
17+
#'
18+
#' srchcol(df = x, col = "z", search = "[a|j]")
19+
#'
20+
#' @autoglobal
21+
#'
22+
#' @export
23+
srchcol <- function(df,
24+
col,
25+
search,
26+
ignore = TRUE,
27+
...) {
28+
dplyr::filter(
29+
df,
30+
stringr::str_detect(
31+
!!rlang::sym(
32+
col
33+
),
34+
stringr::regex(
35+
search,
36+
ignore_case = ignore
37+
)
38+
)
39+
)
40+
}

README.Rmd

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ knitr::opts_chunk$set(
1818
)
1919
```
2020

21-
# fuimus <a href="https://andrewallenbruce.github.io/fuimus/"><img src="man/figures/logo.png" alt="fuimus website" align="right" height="200"/></a>
21+
# **fuimus** <a href="https://andrewallenbruce.github.io/fuimus/"><img src="man/figures/logo.png" alt="fuimus website" align="right" height="250"/></a>
2222

23-
> <ins><b>fuimus</b></ins> /ˈfu.i.mus/, [ˈfuɪmʊs̠]
23+
> /ˈfu.i.mus/, [ˈfuɪmʊs̠]
2424
>
25-
> literally, "we have been" in Latin. Often interpreted as "We have endured" or "We have always been", proclaiming the long history of the user. Can often be seen as a family motto (e.g., the Lowlands Scottish [Clan Bruce](https://en.wikipedia.org/wiki/Clan_Bruce)).
25+
> Literally, *"we have been"* in Latin. Often interpreted as *"We have endured"* or *"We have always been"*, proclaiming the long history of the user.
26+
>
27+
> Can often be seen as a family motto (e.g., the Lowlands Scottish [Clan Bruce](https://en.wikipedia.org/wiki/Clan_Bruce)).
2628
2729
<!-- badges: start -->
2830

31+
[![Codecov test coverage](https://codecov.io/gh/andrewallenbruce/fuimus/branch/master/graph/badge.svg)](https://app.codecov.io/gh/andrewallenbruce/fuimus?branch=master)
32+
[![Last commit](https://img.shields.io/github/last-commit/andrewallenbruce/fuimus.svg)](https://github.com/andrewallenbruce/fuimus/commits/master)
33+
[![Code size](https://img.shields.io/github/languages/code-size/andrewallenbruce/fuimus.svg)](https://github.com/andrewallenbruce/fuimus)
34+
[![CodeFactor](https://www.codefactor.io/repository/github/andrewallenbruce/fuimus/badge)](https://www.codefactor.io/repository/github/andrewallenbruce/fuimus)
2935
<!-- badges: end -->
3036

3137
## :package: Installation

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11

22
<!-- README.md is generated from README.Rmd. Please edit that file -->
33

4-
# fuimus <a href="https://andrewallenbruce.github.io/fuimus/"><img src="man/figures/logo.png" alt="fuimus website" align="right" height="200"/></a>
4+
# **fuimus** <a href="https://andrewallenbruce.github.io/fuimus/"><img src="man/figures/logo.png" alt="fuimus website" align="right" height="250"/></a>
55

6-
> <ins>
7-
> <b>fuimus</b>
8-
> </ins>
9-
>
106
> /ˈfu.i.mus/, \[ˈfuɪmʊs̠\]
117
>
12-
> literally, “we have been” in Latin. Often interpreted as “We have
13-
> endured” or “We have always been”, proclaiming the long history of the
14-
> user. Can often be seen as a family motto (e.g., the Lowlands Scottish
15-
> [Clan Bruce](https://en.wikipedia.org/wiki/Clan_Bruce)).
8+
> Literally, *“we have been”* in Latin. Often interpreted as *“We have
9+
> endured”* or *“We have always been”*, proclaiming the long history of
10+
> the user.
11+
>
12+
> Can often be seen as a family motto (e.g., the Lowlands Scottish [Clan
13+
> Bruce](https://en.wikipedia.org/wiki/Clan_Bruce)).
1614
1715
<!-- badges: start -->
16+
17+
[![Codecov test
18+
coverage](https://codecov.io/gh/andrewallenbruce/fuimus/branch/master/graph/badge.svg)](https://app.codecov.io/gh/andrewallenbruce/fuimus?branch=master)
19+
[![Last
20+
commit](https://img.shields.io/github/last-commit/andrewallenbruce/fuimus.svg)](https://github.com/andrewallenbruce/fuimus/commits/master)
21+
[![Code
22+
size](https://img.shields.io/github/languages/code-size/andrewallenbruce/fuimus.svg)](https://github.com/andrewallenbruce/fuimus)
23+
[![CodeFactor](https://www.codefactor.io/repository/github/andrewallenbruce/fuimus/badge)](https://www.codefactor.io/repository/github/andrewallenbruce/fuimus)
1824
<!-- badges: end -->
1925

2026
## :package: Installation

codecov.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
comment: false
2+
3+
coverage:
4+
status:
5+
project:
6+
default:
7+
target: auto
8+
threshold: 1%
9+
informational: true
10+
patch:
11+
default:
12+
target: auto
13+
threshold: 1%
14+
informational: true

man/display_long.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/gh_raw.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)