Skip to content

Instantly share code, notes, and snippets.

View matt-dray's full-sized avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
@matt-dray
matt-dray / generate_num_format.R
Created May 28, 2024 19:54
Quick function to split a number and convert it to an Excel-style numFmt
generate_num_format <- function(number = 8634567.890) {
num_val <- number %/% 1
num_dp <- number %% 1
num_split <- strsplit(as.character(number), "\\.")[[1]]
n_dp <- nchar(num_split[2])
num_commas <- scales::number(as.numeric(num_val), big.mark = ",")
fmt_out <- gsub("[[:digit:]]", "#", num_commas)
@matt-dray
matt-dray / github-tag-and-release.md
Created May 24, 2024 14:21
Quick step-by-step to tag and release via GitHub
  1. On the repo homepage, click the text 'Releases' in the right-hand section.
  2. Click the 'Draft a new release' button (upper-right).
  3. Click the 'Choose a tag' button (upper-left). This is going to 'tag' this point in the Git history with the version number.
  4. Type in the box the version you're releasing, e.g. 'v0.1.1'.
  5. Click '➕ Create new tag: v0.1.1 on publish' that appears under the box.
  6. Click 'Generate release notes', which will autoname the release with the tag autofill the notes with a bullet per PR since the last release
  7. Click 'Publish release' button (lower-right) to publish the release and add the tag to the version history.
  8. Return to the repo's homepage, where you'll see that the version number has incremented under the 'Releases' section.
@matt-dray
matt-dray / github-etiquette.md
Last active May 17, 2024 12:02
Notes for a 'GitHub Etiquette' talk

Title

  • GitHub etiquette
  • GitHub in a small team
  • GitHub: what works for us
  • A loose GitHub playbook
  • GitHub is a team sport

Angle

@matt-dray
matt-dray / external-data-usethis.md
Last active June 5, 2024 16:03
Steps to create an exported dataset in an R package with {usethis}
  1. In your package project, run usethis::use_data_raw("demo-data") to set up a data-raw/ folder with a demo-data.R file inside.
  2. Write a script in demo-data.R to produce the data object (e.g. demo_df).
  3. Insert and run the line usethis::use_data(demo_df) in demo-data.R, which will save the data object to a data/demo_df.rda file.
  4. Run usethis::use_r("demo-data") to create a corresponding R/demo-data.R file where you can document the data.
  5. In R/demo-data.R, quote the name of the data object (i.e. "demo_df") and put {roxygen2} code above it (probably at least @title, @description and maybe @format, which might contain a \describe{} block to explain the content of your object, itself containing an \item{} to describe each column if it's a data.frame).
  6. Run devtools::document() to generate the man/ pages for the data.
  7. Run devtools::load_all() to reload your package and make demo_df available in your session.
  8. Once pushed, users can attach the package and acc
/*! jQuery v3.2.1 | (c) JS Foundation and other contributors | jquery.org/license */
/*! Adapted from https://jeroen.github.io/clippy/bundle.js by Jeroen Ooms */
!function(a,b){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){"use strict";var c=[],d=a.document,e=Object.getPrototypeOf,f=c.slice,g=c.concat,h=c.push,i=c.indexOf,j={},k=j.toString,l=j.hasOwnProperty,m=l.toString,n=m.call(Object),o={};function p(a,b){b=b||d;var c=b.createElement("script");c.text=a,b.head.appendChild(c).parentNode.removeChild(c)}var q="3.2.1",r=function(a,b){return new r.fn.init(a,b)},s=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,t=/^-ms-/,u=/-([a-z])/g,v=function(a,b){return b.toUpperCase()};r.fn=r.prototype={jquery:q,constructor:r,length:0,toArray:function(){return f.call(this)},get:function(a){return null==a?f.call(this):a<0?t
@matt-dray
matt-dray / r-tables.txt
Last active April 16, 2024 17:57
Packages for making tables in R
arsenal::tableby()
DT
flextable
formattable
gt
huxtable
knitr::kable()
kableExtra
mmtable2
modelsummary
@matt-dray
matt-dray / general-cell-nmft.R
Last active April 16, 2024 08:17
Testing 'general' cell number format with {openxlsx2}
x <- palmerpenguins::penguins |>
tidyr::drop_na() |>
dplyr::mutate(
sp = species,
bill = bill_length_mm,
sex = as.character(sex),
.keep = "none"
) |>
head() |>
as.data.frame()
@matt-dray
matt-dray / expand-nested-list.Rmd
Created April 12, 2024 13:27
An experiment to expand a nested list into Rmd sections, including a recursive function to remove blank (`""`) elements
---
title: "Autogenerating Rmd sections from a nested list"
output:
html_document:
code_folding: hide
---
```{r}
remove_blanks_recursively <- function(x) {
@matt-dray
matt-dray / input-checks-rlang-cli.R
Created March 8, 2024 09:47
Example functions to check inputs to a parent function, using {cli} for better messaging and {rlang} for dot collection and to prevent an 'error handling eclipse'
#' Check Class of Argument Inputs
#' @param ... Objects to be checked for class.
#' @param .expected_class Character. The name of the class against which objects
#' @param .call Environment. The environment in which this function is called.
#' will be checked.
#' @noRd
check_class <- function(
...,
.expected_class = c("numeric", "character"),
.call = rlang::caller_env()
@matt-dray
matt-dray / sifter-abstract-assignment.R
Last active March 7, 2024 12:07
Assign abstracts to sifters as equally as possible so that each abstract is reviewed exactly n times
# As above, but checks for exact name and affiliation matches between
# sifters and abstracts.
#
# It's possible certain abstracts might not get assigned, especially given combos
# of sifters writing abstracts, abstracts from sifters' affiliations, assignment
# capping and sifter capping. Should probably report any abstracts that have
# <assignment_cap assignments. As a precaution, have built in a max_iterations arg
# in case of infinite looping, but I don't think that will come into play.
.resample <- function(x, ...) x[sample.int(length(x), ...)] # see ?sample