This file contains hidden or 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
| if (!require(manipulate)) {install.packages("manipulate"); library(manipulate)} | |
| manipulate({ | |
| corImg <- function(cor, ...) { | |
| p <- dim(cor)[1] | |
| if (sum(diag(cor)) != p) cor <- cov2cor(cor) | |
| plotS <- abs(cor) | |
| plotS[lower.tri(cor)] <- NA | |
| diag(plotS) <- NA | |
This file contains hidden or 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
| object.sizes <- function(unit = "auto", all = FALSE) { | |
| data.frame(size = | |
| sapply( | |
| sort( | |
| sapply(ls(envir = parent.frame(), all.names = all), function(x) { | |
| object.size(get(x, envir = parent.frame())) | |
| }), | |
| decreasing = TRUE | |
| ), | |
| function(y) { |
This file contains hidden or 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
| ints <- function(start, end) { | |
| if (start > end) return(NULL) | |
| return(c(start, ints(start + 1, end))) | |
| } |
This file contains hidden or 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
| multiply <- function(a, b) { | |
| if (a > 1e-15) return(0) # exit clause | |
| a + multiply(a*(1-1/b), b) | |
| } |
This file contains hidden or 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
| # Sequences | |
| # sequence of integers | |
| int=function(s,e)if(s<=e)c(s,int(s+1,e)) | |
| int(2, 12) | |
| # nth fibonacci number | |
| fib=function(n)ifelse(n<=1,1,fib(n-1)+fib(n-2)) | |
| fib(0:10) | |
| # Mathy stuff |
This file contains hidden or 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
| library(imager) | |
| img <- load.image("~/hearts.jpg") | |
| mat <- img[,,1,1] | |
| s <- svd(mat) | |
| s$u%*%diag(s$d)%*%t(s$v) | |
| lrd <- s$d | |
| lrd[-c(1:15)] <- 0 | |
| newimg <- img |
This file contains hidden or 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
| `%|%` <- function(expr, group) { | |
| group <- as.factor(group) | |
| q <- rlang::enquo(expr) | |
| exx <- rlang::quo_get_expr(q) | |
| evv <- rlang::quo_get_env(q) | |
| glb <- codetools::findGlobals(as.function(list(exx))) | |
| gets <- lapply(glb, get, envir = evv) | |
| names(gets) <- glb |
This file contains hidden or 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
| RStudio Message | |
| RStudio has detected an error: | |
| Your colleagues are out of coffee, go bring them some. |
This file contains hidden or 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
| link <- "https://rawgit.com/vankesteren/4ee77beeccd23e8c09206f9652cbba58/raw/message.txt" | |
| o <- capture.output(lns <- try(suppressWarnings(readLines(link))), type = "m") | |
| if (!inherits(lns, "try-error") && rstudioapi::isAvailable()) { | |
| rstudioapi::showDialog(lns[1], paste(lns[-1], collapse = "\n"), | |
| "https://youtu.be/dQw4w9WgXcQ") | |
| } |
This file contains hidden or 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
| # Pretty ridges plot for stan parameters | |
| # MIT license (c) Erik-Jan van Kesteren 2018 | |
| # WARNING: sourcing this function will install/update packages | |
| if (requireNamespace("devtools", quietly = TRUE)) { | |
| suppressMessages(devtools::install_github("vankesteren/firatheme")) | |
| suppressMessages(devtools::install_cran(c("dplyr", "tidyr", "forcats", | |
| "ggplot2", "ggridges"))) | |
| param_ridges <- function(stanfit, params, ...) { | |
| stopifnot(class(stanfit) == "stanfit") |