Skip to content

Instantly share code, notes, and snippets.

View rmflight's full-sized avatar

Robert M Flight rmflight

View GitHub Profile
@gadenbuie
gadenbuie / walk_source.R
Last active February 21, 2020 17:40
Walk an R source file and run each command in RStudio automatically on a delay
walk_source <- function(
file = NULL,
delay = 1.5,
clear_each_cmd = TRUE,
style = styler::tidyverse_style
) {
options("walk_source_cancel" = FALSE)
if (is.null(file)) {
txt <- rstudioapi::getSourceEditorContext()$contents
} else {
@noamross
noamross / partition.R
Last active October 27, 2020 13:13
A quick greedy algorithm to partition unequal-sized groups into near-equal shards
#' Function to partition unequal sized groups into shards of similar size.
#'
#' Based on the greedy algorithm described in [The Wikipedia article on the
#' the partitioning problem](https://en.wikipedia.org/wiki/Partition_problem#The_greedy_algorithm)
#'
#' @param groups_vector An integer vector of group ids, such as a group ID
#' column in a data frame
#' @param n_shards The number of shards to split groups up into
#' @examples
#' n_groups <- 200
@vankesteren
vankesteren / svdreg.R
Last active September 24, 2018 12:29
svd hidim regression
# Try-out of Hastie, Tibshirani, & Friedman (ESL, 2009) p.659
# Generate high-dimensional dataset
beta <- -100:100/100
X <- matrix(rnorm(100*201), 100)
y <- X%*%beta + rnorm(100, 0, sqrt(crossprod(beta)))
# Ridge estimates
bhat_ridge <- solve(crossprod(X) + diag(rep(.1, 201)), crossprod(X, y))
@oliviaguest
oliviaguest / remove_non_mutuals.py
Last active August 1, 2019 08:44
Remove (and backup) non-mutuals on Twitter.
#!/usr/bin/env python3
import csv
import time
import tweepy
import datetime
# Read http://docs.tweepy.org/en/v3.5.0/auth_tutorial.html to fill in the two lines below.
auth = tweepy.OAuthHandler('XXX', 'YYY')
auth.set_access_token('123', '456')
@bearloga
bearloga / clickable-imgs.js
Created September 28, 2016 19:22
Make the figures click-able in your RMarkdown=>HTML reports.
<script language="JavaScript">
$(function() {
/* Lets the user click on the images to view them in full resolution. */
$("div.figure img").wrap(function() {
var link = $('<a/>');
link.attr('href', $(this).attr('src'));
link.attr('title', $(this).attr('alt'));
link.attr('target', '_blank');
return link;
});
@klmr
klmr / deploy.sh
Last active June 9, 2016 19:54
Deploy a generated GitHub page (`_site` subfolder)
#!/usr/bin/env bash
# Configuration
# The remote target branch name needs to be
# 1. DIFFERENT from the local development branch
# 2. Set as the default branch name on GitHub
remote_target_branch=master
generated_contents_dir=_site
get-working-branch() {
strip_glm <- function(cm) {
cm$y = c()
cm$model = c()
cm$residuals = c()
cm$fitted.values = c()
cm$effects = c()
cm$qr$qr = c()
cm$linear.predictors = c()
cm$weights = c()
@rmflight
rmflight / code_counting.md
Last active July 24, 2020 00:26
stuff I always need to find

If you want to know how many lines of code are in an Rmd file:

rmarkdown::render("file.Rmd", output_format = "md_document")

cat file.md | grep -c '^ \{4\}'

And then how many lines of code in .R files:

@gaborcsardi
gaborcsardi / cran-over-time.R
Last active July 18, 2020 19:21
Size of the CRAN R package repository over time
library(jsonlite)
## Download
pkgs <- fromJSON("http://crandb.r-pkg.org/-/events")
## Filter
na_pkgs <- unique(pkgs$name[ is.na(pkgs$date) ])
events <- pkgs[ ! pkgs$name %in% na_pkgs, c("date", "name", "event")]
@hussius
hussius / kallisto_setup.sh
Last active December 11, 2020 15:45
Kallisto setup
# Download Kallisto and sratools (the latter to be able to download from SRA)
wget https://github.com/pachterlab/kallisto/releases/download/v0.42.3/kallisto_mac-v0.42.3.tar.gz
tar zvxf kallisto_mac-v0.42.3.tar.gz
wget http://ftp-trace.ncbi.nlm.nih.gov/sra/sdk/2.5.2/sratoolkit.2.5.2-mac64.tar.gz
tar zxvf sratoolkit.2.5.2-mac64.tar.gz
# Download and merge human cDNA and ncDNA files from Ensembl for the index.
wget ftp://ftp.ensembl.org/pub/current_fasta/homo_sapiens/cdna/Homo_sapiens.GRCh38.cdna.all.fa.gz
wget ftp://ftp.ensembl.org/pub/current_fasta/homo_sapiens/ncrna/Homo_sapiens.GRCh38.ncrna.fa.gz
cat Homo_sapiens.GRCh38.cdna.all.fa.gz Homo_sapiens.GRCh38.ncrna.fa.gz > Homo_sapiens.GRCh38.rna.fa.gz