Skip to content
JesseAlderliestenPublic

About

Programming Utilities

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

progutils

R-CMD-check R-CMD-check-no-suggests

progutils contains utility functions, for example to reorder values, check equality, construct messages, or handle files and directories.

Installation

Visit the progutils website to explore the package, or install progutils from GitHub using the following R code:

if(!requireNamespace("remotes")) {
  install.packages(pkgs = "remotes")
}
remotes::install_github(repo = "JesseAlderliesten/progutils",
                        upgrade = FALSE, lib = NULL)

For more information about installing and configuring R, see my package checkrpkgs.

Examples

Every once in a while, reading data into R results in a factor that should be converted to a numeric vector. The code below illustrates that using as.numeric() from base R does not work to achieve this while using as.numeric_safe() from progutils does work.

library(progutils)
x <- factor(11:13)
x
#> [1] 11 12 13
#> Levels: 11 12 13
as.numeric(x) # Returns the indices instead of the values!
#> [1] 1 2 3
as.numeric_safe(x) # Returns the values.
#> [1] 11 12 13

Similarly, relevel() from base R can only be used to assign a reference level to a factor and using levels() from base R does not work to reorder factor levels, whereas using reorder_levels() from progutils does work:

library(progutils)
f_orig <- factor(letters[c(12:13, 13:11)], levels = letters[13:11])
f_orig
#> [1] l m m l k
#> Levels: m l k

f_test <- f_orig
levels(f_test) <- letters[11:13]
f_test # Also changes the values!
#> [1] l k k l m
#> Levels: k l m
# Only changes the levels:
reorder_levels(x = f_orig, new_order = letters[11:13])
#> [1] l m m l k
#> Levels: k l m

To use a named numeric vector in the subtitle of a plot, the vector should be converted to a character string wrapped to an appropriate length, to prevent text from running off the plot. progutils provides a way to achieve that by combining vect_to_char() and wrap_text():

library(progutils)
msg_part_one <- "Some\nmessage you want to display"
num_vect <- c(j = 10, k = 11, l = 12)

cat(
  wrap_text(
    paste(msg_part_one, vect_to_char(num_vect)),
    width = 20)
)
#> Some message you
#> want to display j:
#> 10, k: 11, l: 12

# Retaining newlines that were present in the original message:
cat(
  wrap_text(
    paste(msg_part_one, vect_to_char(num_vect)),
    width = 20,
    ignore_newlines = FALSE)
)
#> Some
#> message you want to
#> display j: 10, k:
#> 11, l: 12

License

This project is licensed under the terms of the MIT License.

Citation

To cite package 'progutils' in publications use:

  Alderliesten J (2026). _progutils: Programming Utilities_. R package
  version 1.0.1, <https://github.com/JesseAlderliesten/progutils>.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {progutils: Programming Utilities},
    author = {Jesse Alderliesten},
    year = {2026},
    note = {R package version 1.0.1},
    url = {https://github.com/JesseAlderliesten/progutils},
  }

Contact

Please open a GitHub issue if you have suggestions to improve progutils.

Similar packages

Many packages with miscellaneous functions exist on GitHub and CRAN: frequently they have ‘misc’ or ‘util’ in their title or description. In addition, many packages have an ‘utils’ folder inside their R folder.

About

Programming Utilities

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages