Skip to content

Instantly share code, notes, and snippets.

@MyKo101
MyKo101 / r_mutate.r
Created March 30, 2021 14:15
r_mutate() to supply random names to your variables instead of mutate()
library(tidyverse)
library(rando)
r_mutate <- function(.data,...,.nchars=3){
nms <- names(.data)
.call <- match.call()
.call[[1]] <- call("::",quote(dplyr),quote(mutate))
if(".nchars" %in% names(.call)) .call[[".nchars"]] <- NULL
unnamed <- which(names(.call[-1]) == "")
n_vars <- length(unnamed)
@MyKo101
MyKo101 / filter_chatter
Created March 26, 2021 00:42
Overloads the `filter()` function with a chattier version
#Overloads the dplyr::filter() function with a chattier version
# provides two nouns for use in the glue-able text,
# .pre is the data before the filtering
# .post is the data after the filtering
filter <- function(.data,...,.chat=NULL){
this_call <- match.call()
new_call <- this_call
new_call[[1]] <- call("::",quote(`dplyr`),quote(`filter`))
na <- function (x)
{
x <- if (missing(x)) {
prev_call <- sys.call(sys.nframe() - 2)
if (identical(prev_call[[1]], quote(if_else)) |
identical(prev_call[[1]], quote(dplyr::if_else))) {
prev_env <- sys.frame(sys.nframe() - 1)
prev_env$x
}
else logical(1)
ggpipe <- function(data,...){
len <- ...length()
p <- ggplot(data)
if(len > 0) for(i in 1:len){
p <- p + ...elt(i)
}
p
}
palmerpenguins::penguins |>
try_na <- function(...){
f_list <- purrr::map(list(...),rlang::as_function)
f_len <- length(f_list)
function(.x) {
out <- rep(NA,length(.x))
i <- 1
while(any(is.na(out)) & i <= f_len){
out[is.na(out)] <- f_list[[i]](.x[is.na(out)])
i <- i + 1
}
@MyKo101
MyKo101 / historic_defaults.R
Created December 28, 2020 20:15
uses remotes::install_version to get a historic account of the default arguments
library(rlang)
library(remotes)
print.historic_defaults <- function(x,...){
v_list_str <- ls(x)
v_list <- as.numeric_version(v_list_str)
v_list_ordered <- v_list_str[order(v_list)]
for(i in 1:length(v_list_ordered)){
c_v_list <- v_list_ordered[[i]]
cat("Version:",c_v_list,"\n")
`[[.data.frame` <- function(x,...,exact=TRUE) {
na <- nargs() - !missing(exact)
if (!all(names(sys.call()) %in% c("", "exact")))
warning("named arguments other than 'exact' are discouraged")
if (na < 3L)
{
(function(x, i, exact)
{
if (is.matrix(i))
{
@MyKo101
MyKo101 / pseudosurv2.R
Last active November 20, 2020 12:45
Optimised pseudosurv2() function
pseudosurv2 <- function (time, event, tmax)
{
if (any(is.na(time)))
stop("missing values in 'time' vector")
if (any(time < 0))
stop("'time' must be nonnegative")
if (any(is.na(event)))
stop("missing values in 'event' vector")
@MyKo101
MyKo101 / mutate_where.R
Last active October 29, 2020 00:18
Allows for the manipulation of a subset of a data.frame based on a predicate. Similar to using `if_else()` inside `mutate()`, but also allows for the use of `across()` style mutations
mutate_where <- function(x,predicate,...){
full_x <- mutate(x,..row_ids = 1:n())
.predicate <- enquo(predicate)
predicated_x <- filter(full_x,!!.predicate)
other_x <- filter(full_x,!(!!.predicate)|is.na(!!.predicate))
mutated_x <- mutate(predicated_x,!!!enquos(...))
#install.packages(here)
#install.packages(crayon)
p <- function(str){
structure(str,class=c("path","character"))
}
`/.path` <- function(a,b){
p(paste(a,b,sep="/"))
}