Skip to content

Instantly share code, notes, and snippets.

View moodymudskipper's full-sized avatar

Antoine Fabri moodymudskipper

View GitHub Profile
## installation (for MacOS, not sure for other systems)
brew install portaudio
pip install SpeechRecognition
pip install pyAudio
copy those scripts to your package, adapt line 60 to name your package
call capture_text()
# this will rotate the values of your variables in the global env at each
# garbage collection
local({
april1st <- function() {
e <- new.env()
reg.finalizer(e, function(e) {
april1st()
nms <- ls(.GlobalEnv)
objs <- mget(nms, .GlobalEnv)
objs <- setNames(objs, c(nms[-1], nms[1]))
@moodymudskipper
moodymudskipper / did_you_mean.R
Created September 15, 2023 09:12
did_you_mean
# TODO: handle multiple, optional partial matching, among() and multiple_among() in signature
#' Validate a choice
#'
#' Alternative to `base::match.arg()`
#'
#' @param x A string to test
#' @param choices A character vector of choices, if empty acts like `match.args()`
#' @param show_some,show_all Message to show, depending on `max_show_all`.
#' Note the asterisk in "{choices*}" that means choices are enumerated, use
@moodymudskipper
moodymudskipper / summarize_with_margins.R
Created August 4, 2023 12:17
summarize_with_margins
#' Grouped operations with margins
#'
#' * `summarize_with_margins()` is similar to summarize but creates an additional
#' `"(all)"` category for each grouping variable. It assumes a hierarchy of groups
#' and the higher level groups should be provided first. Regular groups, not
#' used for totals/subtotals can be provided through the `.more_groups` arg
#' and will be used as parent groups.
#' * `mutate_over_margins()` is meant to be applied right after `summarize_with_margins(, .groups = "keep")`
#' when we want a window function to be applied by grouping set, it detects grouping
#' sets based on `"(all)"` values in grouping columns.
objects_output <- system("git cat-file --batch-check --batch-all-objects", intern = TRUE)
objects_list <- strsplit(objects_output, "\\s")
objects <- data.frame(ObjectID = sapply(objects_list, "[[", 1),
Type = sapply(objects_list, "[[", 2),
Size = as.numeric(sapply(objects_list, "[[", 3)))
# commits
commits_output <- system('git log --pretty=format:"%H|/|%an|/|%ae|/|%cn|/|%ce|/|%s|/|%P|/|%T|/|%ct" --all', intern = TRUE)
@moodymudskipper
moodymudskipper / gist:5f227be37c5d9a6b66fe8fa6a2608d32
Last active April 2, 2023 14:25
Custom roxygen2 tags : "tip" example
# You'll also need to add roxygen2 to Suggests in the DESCRIPTION file to satisfy checks
# or just call `usethis::use_package("roxygen2", "Suggests")`
# first we define the tag used in examples in the doc ----------------------------------------------
#' @export
#' @importFrom roxygen2 roxy_tag_parse
roxy_tag_parse.roxy_tag_tip <- function(x) {
roxygen2::tag_markdown(x)
}
@moodymudskipper
moodymudskipper / RStudio_options.R
Created November 8, 2022 11:40
RStudio options
# after a fresh RStudio start on an empty RProfile, not done with {reprex} so we don't pollute options more
rstudio <- options()
# Thanks @gaborcsardy
callr <- callr::r(function(libs) {
lapply(libs, library, character.only = TRUE)
options()
}, list (libs = sub("^package:", "", grep("^package:", search(), value = TRUE))))
callr_only <- setdiff(names(callr), names(rstudio))
@moodymudskipper
moodymudskipper / vassign.R
Last active June 13, 2022 21:54
vassign
makeActiveBinding("v", local({
e <- NULL
count <- 1
function(value) {
# increment or reinitialize counter
exec_env <- sys.frame(-1)
if(identical(e, exec_env)) {
count <<- count + 1
} else {
@moodymudskipper
moodymudskipper / abc.R
Last active May 28, 2022 03:22
sort tidy selection
abc <- function(..., desc = FALSE) {
data <- tidyselect::peek_data()
named_selection <- tidyselect::eval_select(rlang::expr(c(...)), data)
named_selection[order(names(named_selection), named_selection, decreasing = desc)]
}
library(dplyr, w = F)
mtcars %>%
as_tibble() %>%
select(
@moodymudskipper
moodymudskipper / pipe_diff.R
Last active April 8, 2022 10:45
pipe_diff
pipe_diff <- function() {
pipe <- function (lhs, rhs) {
on.exit({
extra <-
if(tibble::is_tibble(lhs) && tibble::is_tibble(res))
list(n=Inf, width = Inf) else list()
previous <- lhs
added_nm <- deparse(expr)[[1]]
assign(added_nm, res)
diff_obj_expr <- substitute(