Desiderata is a personal package with a bunch o' functions that I (Desi) have collected or written as I needed them.
install.packages("remotes")
remotes::install_github("DesiQuintans/desiderata")
library(desiderata)
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
desiderata::
ensure()
--- Quick assertion function for QA checks on code (e.g. checking that a dataframe has the same number of rows before and after a join)Show()
--- Pipeline- and knit-friendly View()
clippy()
--- Copy a dataframe, vector, or the result of an expression to the clipboardtry.seed()
--- Generate random seeds to preview their effectscoinflip()
--- Randomly return TRUE
or FALSE
Mode()
--- Find the mode(s) of a numeric/character/factor vectorna_rm()
--- Remove NA
from vectors, lists, matrices, and dataframeshowmany()
--- Count the number of unique elementscount_unique()
--- Count how many times each unique element in a vector is repeatedunique_n()
--- Keep the first n
unique elements in a vectorkeep_every()
--- Keep every other element of a vector, flexiblyassign_groups()
--- Assign elements in a vector to groupssplit_size()
--- Split a vector into chunks of size n
%notin%
--- Flag vector elements that are not in another vector (inverse of %in%
)%pctin%
--- Percentage of matching elements between two vectorsnot.na()
--- Opposite of is.na()
not.nan()
--- Opposite of is.nan()
if_na()
--- Alias for ifelse(is.na(x), yes = TRUE, no = x)
.fold()
--- Friendly printing of multi-item vectors, e.g. "Jan, Feb, and 10 others"
.anyFalse()
--- Alias of any(!(...))
(note: it's different from !any(...)
)report_diff()
--- Are these two vectors exactly the same? What pairwise elements are mismatched, and how many?overwrite_df()
--- Replace all matching values in a dataframe with something elsedrop_empty_cols()
--- Drop 'empty' columns in a dataframe (plus regex matching)drop_empty_rows()
--- Drop 'empty' rows in a dataframe (plus regex matching)collapse_df()
--- Collapse a dataframe's cells into a single vectorsort_cols()
--- Sort columns of a dataframe by namedrop_invar_cols()
--- Drop invariant columns from a dataframetop_tail()
--- First and last rows of a dataframeadd_group_size()
--- Add group size as a columnrows_with_na()
--- Only keep rows that contain NA
same_cols()
--- Given two dataframes, which columns appear in both of them?diff_cols()
--- Given two dataframes, which columns are present in one but not in the other?na_in_row()
--- Gives the count/proportion of NA
for each row of a dataframe, using dplyr
tidyselectors (-
, :
, starts_with()
, etc.) to choose which columns to look at.round_to_nearest()
--- Round numbers to the nearest "pretty" value (e.g. nearest 0.1
)round_to()
--- Round numbers to a fixed number of decimal places and return them as Numeric
normalize_colwise()
--- Normalise a matrix column-wise between 0 and 1normalize_whole()
--- Normalise a whole matrix between 0 and 1concat_nums()
--- Concatenate a numeric vector into a single numbermirror_matrix()
--- Mirror a matrix horizontallyis.prime()
--- Is a number prime?encode_signif()
--- Replace p-values with significance codesmark_longest_run()
--- Find and mark the longest run of TRUEs in a boolean vectormark_last_peak()
--- Mark the location of the last maximum value (peak) in a vectoromit_dips()
--- Replace element with NA
if it is less than the elements before and after itis.flanked()
--- Is a vector element surrounded by certain values?cut_int()
--- Cut an integer into n
exclusive chunks, e.g. chunk 1 gets rows 1-10, chunk 2 gets rows 11-20, and so on.geomean()
--- Geometric mean of a vectorse_mean()
--- Standard error of the meandegreedays()
--- Calculate degree-dayspercentile()
--- Quick percentile overview (plus plotting option)cumpct()
--- Cumulative percentageIQR_outliers()
--- Mark outliers in a vector according to the percentile methodtriangle_num()
--- nth Triangle Number, like factorial but with additionboot_ci_mean()
--- Bootstrapped confidence interval of the meaninstant_runoff_voting()
--- Instant run-off (ranked-choice) voting algorithmvec_to_regex()
--- Collapse vectors into a regex patternstr_coll()
--- Collapse a vector into a single stringuw()
and uw0()
--- "Unwrap" hard-wrapped strings into a single linecommon_stem()
--- Return the stem that is common to a set of stringsstr_rev()
--- Reverse the elements of a vector (e.g. make words go backwards) without changing the order of elements in the vector itself.rev_sentence()
--- Reverse the order of words in a stringinterleave()
--- Interleave one string with anothermatch_in_substr()
--- Match strings that have a needle near the start, end, or middleconsecutive_month()
--- Find the current month number relative to a starting dateconsecutive_week()
--- Find the number of weeks or isoweeks between two datesround_to_duration()
--- Round a duration of days to other units (e.g. weeks, months).show_colours()
--- Plot colours as tilesbuild_palette()
--- Manipulate a list of hex colourspalette_builtin()
--- A palette of R's 502 distinct built-in colourspalette_distinct()
--- A palette of 1,022 visually-distinct colourspalette_mrmrs()
--- A palette of Adam Morse's 16 web-safe colourspalette_picked()
--- A palette of 14 hand-picked distinct coloursplot_arrange()
--- Arrange base R plots on a gridrcols_as_hex()
--- Convert R's built-in named colours to hex codescol2hsv()
--- Convert hex colours to HSVquick_lm()
--- Fit and plot a two-term linear model quicklytheme_desi_base()
--- Desi's ggplot2
minimal base themealign_titles()
--- Horizontally align ggplot2 title and subtitlerotate_x_text()
and rotate_y_text()
--- Rotate and align ggplot2 axis tick labelsrotate_x_facet_text()
and rotate_y_facet_text()
--- Rotate and align ggplot2 facet labelsdendro()
--- Print a dendrogramloadRDS()
--- Load an RDS file and announce when it was createdmake_path()
--- Build a path, creating subfolders if neededapply_to_files()
--- Apply a function to every file in a folder that matches a regex patternwrite_csv_rds()
--- Write a dataframe to a CSV and RDSsave_a4()
--- Save a ggplot as an A4 imageshush()
--- Suppress all console printing (cat
, print
, warning
, message
)cat_wrap()
--- Print to console, wrapping the text to a specific line widthdots_char()
--- Convert dots to a character vector or a stringset_seed_any()
--- Seed the random number generator with a character string (or any object)basic_color_names
--- The names of 197 common colours: red, green, cyan, etc.random_integers
--- 10,000 random integers between 0 and 100iris_labelled
--- The iris
dataset with a label
attribute added, as Hmisc
and other packages do.nz_boty_2019_head
--- The first 1000 voters from the New Zealand Bird of the Year survey, 2019.material2014_colblind
--- The Material Design 2014 colour palette, plus simulated colourblind conversions via khroma:::anomalize()
.show_colours(palette_builtin())
Many of these are not colorblind safe, and many of them have low contrast or are very similar (but should still be different enough to discriminate when they are side-by-side).
show_colours(palette_distinct())
This is a palette by Adam Morse, sourced from https://clrs.cc/.
show_colours(palette_mrmrs())
The palette created by desiderata::palette_distinct()
has a lot of colours that are either so dark or so light that it's difficult to differentiate them next to each other. In addition, many of the colours are affected by adjacency effects, where they cannot be differentiated when they're next to a closely-related colour.
I went through the preview plots manually, randomising the order of the colours each time and deleting any colours that were visually similar until I ended up with a list of colours that were easy to differentiate.
show_colours(palette_picked())
ggplot2
minimal base themeggplot(iris, aes(x = Petal.Length, y = Sepal.Length)) +
geom_point() +
theme_desi_base()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.