Skip to content

Instantly share code, notes, and snippets.

@MyKo101
MyKo101 / multi_case_when()
Last active July 15, 2020 00:16
Function to perform case_when() to produce multiple colums
food <- tibble(item=c("apple","baguette","zucchini","mushrooms"))
multi_case_when <- function(.x,.names,...) {
require(rlang)
require(magrittr)
require(purrr)
.dots <- enquos(...)
@MyKo101
MyKo101 / show_function()
Created July 15, 2020 13:17
Function to open the function in a new file editor
show_function <- function(fun){
tmp_dir <- tempdir()
fun_expr <- enexpr(fun)
if(rlang::is_call(fun_expr) &&
(identical(fun_expr[[1]],quote(`::`)) || identical(fun_expr[[1]],quote(`:::`)))) {
fun_name <- rlang::as_name(fun_expr[[3]])
} else {
fun_name <- rlang::as_name(fun_expr)
}
@MyKo101
MyKo101 / datatable_subsetters
Created August 21, 2020 23:18
`subsetters` operators for `data.tables`
`[[.data.table` <- function(data,subset){
subset(data,eval(subset,data))
}
subsetter <- function(x){
new_subsetter(substitute(x))
}
new_subsetter <- function(x){
@MyKo101
MyKo101 / mutate_s.R
Created August 30, 2020 20:43
mutate_s() function to keep attributes during mutate()
mutate_s <- function(.data,...){
if(!inherits(.data,"data.frame")) stop("mutate_s() can only act on data.frames")
require(rlang)
.dots <- rlang::enexprs(...)
lhs <- names(.dots)
k <- length(.dots)
p_env <- parent.frame()
#install.packages(here)
#install.packages(crayon)
p <- function(str){
structure(str,class=c("path","character"))
}
`/.path` <- function(a,b){
p(paste(a,b,sep="/"))
}
@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(...))
@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")
`[[.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 / 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")
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
}