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
import itertools | |
from dataclasses import dataclass | |
from typing import Tuple | |
import numpy as np | |
from diptest import diptest | |
MEAN = 18.75 | |
STDDEV = 5.647 |
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 function will calculate whether a vector is prime. It is not completely efficient, but it's not too bad | |
isPrime <- function(n){ | |
# Check inputs are integers, and coerce | |
if(any(n %% 1 >0)) stop("Input must be all integers") | |
n2 <- as.integer(n) | |
# Create default output | |
out <- rep(NA,length(n2)) | |
# Check for already discovered primes & return early if possible |
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
# Define a character to act as seperator between the two variables to be used | |
hb_sep <- function(x=NULL){ | |
if(is.null(x)){ | |
getOption("hb_sep") | |
} else { | |
options(hb_sep = x) | |
} | |
} | |
# Combines the hue & brightness |
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
#' Reload .Rprofile | |
#' | |
#' Re-run the .Rprofile file | |
#' | |
#' @interactive | |
#' @shortcut Ctrl + Shift + P | |
function(){ | |
source(".Rprofile") | |
cat("Reloading .Rprofile\n") | |
} |
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
temp_env <- new.env() #Create a new environment | |
if("LoadedEnv" %in% search()) detach("LoadedEnv") #If this has previously been run, remove it! | |
ff <- list.files("functions",full.names=TRUE) #Load up the files in the functions folder | |
silencer <- lapply(ff,source,local=temp_env) #source them all into the temporary environment | |
attach(temp_env,name="LoadedEnv") #Add the environment to the search path | |
rm(temp_env,ff,silencer) #Clean up |
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
#' Apply on.exit() anywhere | |
#' | |
#' Apply the on.exit() functionality to any environment | |
#' | |
#' Very useful for creating and closing temporary connections (see examples) | |
#' | |
#' @examples | |
#' local_sink <- function(file){ | |
#' sink(file) | |
#' on.exit.env(sink(),env=parent.frame()) |
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
#' Create a delayed evaluation of a call | |
#' | |
#' Works similarly to `delayAssign()`, except it works as part of an assignment. | |
#' Can only be called from within a function and must be directly part of a left-assignment | |
#' This can be used to pass things like R CMD Check for delayed variables, rather than | |
#' using `delayedAssign("x",call)` is the equivalent of `x <- delayed_variable(call)` | |
delayed_variable <- function(call){ | |
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
listframe <- function(...){ | |
structure( | |
tibble(...), | |
class = c("listframe","tbl_df","tbl","data.frame") | |
) | |
} | |
lf <- listframe( | |
a = list(1,c("a","b","c"),matrix(1:4,2,2)), |
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
less_chain <- function(.a,a,b,equal=FALSE){ | |
if(length(a) != length(b) && (length(a) != 1 && length(b) != 1)) | |
stop("Incompatible lengths",call.=FALSE) | |
if(is.call(.a) && | |
(identical(.a[[1]],quote(`%<%`)) || identical(.a[[1]],quote(`%<=%`)))){ | |
mid <- eval(.a[[3]]) | |
rhs <- if(equal) mid <= b else mid < b | |
a & rhs | |
} else if(is.numeric(a) && is.numeric(b)){ | |
if(equal) a <= b else a < b |
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
attribute_lock <- function(x){ | |
if(inherits(x,"attribute_lock")){ | |
x | |
} else { | |
ocl <- attr(x,"class") | |
ncl <- if(!is.null(ocl)) c("attribute_lock",ocl) else "attribute_lock" | |
structure(x,class=ncl) | |
} |
NewerOlder