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 / 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 / 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 / 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
#!/usr/bin/Rscript --vanilla
# from https://github.com/muuankarski/blog
# run ./knitpages.R to update all Rmd files that need to be updated.
KnitPost <- function(input, outfile) {
require(knitr);
opts_knit$set(base.dir = "content")
opts_knit$set(base.url = "/")
@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 / 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 / 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 {
macro compileinfo(file_name, command)
quote
open($(esc(file_name)), "w") do io
ccall(:jl_dump_compiles, Void, (Ptr{Void},), io.handle)
try
$command
finally
close(io)
ccall(:jl_dump_compiles, Void, (Ptr{Void},), C_NULL)
end