Skip to content

Instantly share code, notes, and snippets.

@randy3k
randy3k / xvfb
Created March 3, 2014 23:45 — forked from jterrace/xvfb
XVFB=/usr/bin/Xvfb
XVFBARGS=":1 -screen 0 1024x768x24 -ac +extension GLX +render -noreset"
PIDFILE=/var/run/xvfb.pid
case "$1" in
start)
echo -n "Starting virtual X frame buffer: Xvfb"
start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
echo "."
;;
stop)
@randy3k
randy3k / filter.php
Created April 4, 2014 08:56
wordpress filter
<?php
/*
Plugin Name: My Filter
*/
// Exit if accessed directly
if ( !defined('ABSPATH')) exit;
# to exclude the category testing
function exclude_category($query) {
@randy3k
randy3k / combin.r
Last active November 30, 2017 18:43
# it is a tutorial to generate permuations/combinations using `partitions`, `gtools` and `multicool`.
# mainly for reference only, consider the package `iterpc` in practice.
library(partitions)
library(gtools)
library(multicool)
1) combinations: without replacement: distinct items
n = 5; r = 3
combn(n, r)
@randy3k
randy3k / .bashrc
Last active April 5, 2017 03:21
git aware prompt
function git-branch-name {
echo `git symbolic-ref HEAD --short 2> /dev/null || (git branch | sed -n 's/\* (*\([^)]*\))*/\1/p')`
}
function git-dirty {
[[ `wc -l <<< "$1" ` -eq 1 ]] || echo "*"
}
function git-unpushed {
if [[ "$1" =~ ("behind "([[:digit:]]*)) ]]
then
echo -n "-${BASH_REMATCH[2]}"
@randy3k
randy3k / progress.R
Last active August 29, 2015 14:05
R progress bar
total <- 20
# create progress bar
pb <- txtProgressBar(min = 0, max = total, style = 3)
for(i in 1:total){
Sys.sleep(0.1)
# update progress bar
setTxtProgressBar(pb, i)
}
close(pb)
@randy3k
randy3k / importCpp.R
Last active June 4, 2017 23:12
a wrapper of sourceCpp which keeps the shared library file.
importCpp <- function(infile, output_dir="lib", rebuild=FALSE){
output_dir = ifelse(is.null(output_dir), ".", output_dir)
dir.create(output_dir, recursive=T, showWarnings=FALSE)
outfile = file.path(output_dir, paste0(infile, ".R"))
if (!file.exists(outfile) || file.info(infile)$mtime > file.info(outfile)$mtime || rebuild){
Rcpp::sourceCpp(infile, rebuild=rebuild)
context = .Call("sourceCppContext", PACKAGE = "Rcpp",
normalizePath(infile, winslash = "/"), code=NULL, 0L, .Platform)
scriptfile = file.path(context$buildDirectory, context$rSourceFilename)
@randy3k
randy3k / nbgrader_revision.py
Last active September 13, 2016 04:51
revising released notebooks which are downloaded by students
import nbformat
import os
release_dir = "/home/randy3k/nbgrader/sts437/release"
assignment = "sts437-hw1"
assignment_dir = os.path.join(release_dir, assignment)
problems = [f for f in os.listdir(assignment_dir) if os.path.splitext(f)[1] == ".ipynb"]
users = [d for d in os.listdir("/home") if os.path.isdir("/home/{}".format(d))]
@randy3k
randy3k / rdoc.R
Last active July 25, 2017 04:21
Parsing R documentation file
topic_rd <- function(pkg_name, topic) {
rd_file <- as.character(help((topic), (pkg_name)))
utils:::.getHelpFile(rd_file)
}
topic_section <- function(rd, section) {
ret <- rd[sapply(rd, function(x) attr(x, "Rd_tag") == paste0("\\", section))]
if (length(ret) == 0) {
list()
} else {
@randy3k
randy3k / nginx.conf
Last active August 26, 2018 15:26
jupyterhub and rstudio server setups
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
@randy3k
randy3k / nbviewer.sh
Created February 11, 2017 21:42
shell script running nbviewer
#!/usr/bin/env bash
trap 'kill -TERM $PID' INT TERM EXIT
cd /home/randy3k/nbviewer
python3 -m nbviewer --port=8080 --localfiles=/home/randy3k/ &
PID=$!
wait $PID