Skip to content

Instantly share code, notes, and snippets.

@MyKo101
MyKo101 / diptest.py
Created July 7, 2023 12:09
Multi-Modality Testing
import itertools
from dataclasses import dataclass
from typing import Tuple
import numpy as np
from diptest import diptest
MEAN = 18.75
STDDEV = 5.647
@MyKo101
MyKo101 / isPrime.R
Created November 9, 2022 20:25
Function to calculate whether a vector is prime.
# 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
@MyKo101
MyKo101 / scale_colour_hb.R
Created September 9, 2021 21:55
colour ggplot with hue & brightness
# 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
@MyKo101
MyKo101 / Load_Rprofile_shortcut
Created September 2, 2021 20:48
Reload Rprofile shortcut
#' Reload .Rprofile
#'
#' Re-run the .Rprofile file
#'
#' @interactive
#' @shortcut Ctrl + Shift + P
function(){
source(".Rprofile")
cat("Reloading .Rprofile\n")
}
@MyKo101
MyKo101 / load_environment.R
Last active September 2, 2021 20:42
Load Folder as Environment
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
@MyKo101
MyKo101 / on.exit.env.R
Created July 6, 2021 11:04
Apply on.exit() to any environment, useful for parent frames
#' 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())
#' 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){
@MyKo101
MyKo101 / mutate.listframe
Last active April 19, 2021 09:47
Application of the mutate method to a listframe
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)),
@MyKo101
MyKo101 / less_chain
Created April 18, 2021 20:27
Allow less thans in a chain
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
@MyKo101
MyKo101 / attribute_lock
Created April 18, 2021 19:53
locks attributes for combination
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)
}