This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apple_screenshot <- function(file = tempfile(fileext = ".png")) { | |
cmd <- sprintf("screencapture -xC %s", file) | |
system(cmd) | |
invisible(file) | |
} | |
apple_keystroke <- function(x) { | |
cmd <- sprintf( | |
"osascript -e 'tell application \"System Events\" to keystroke \"%s\"'", | |
x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( |
NewerOlder