Skip to content

Instantly share code, notes, and snippets.

@cboettig
cboettig / rename.sh
Created August 28, 2011 17:31
Change file extensions in bash
#!/bin/bash
for f in *.JPG; do mv "$f" "`basename "$f" .JPG`.jpg"; done;
@cboettig
cboettig / lsos.R
Created September 1, 2011 20:21
List data size of objects in R
## Credit: Taken from: http://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session
# improved list of objects
.ls.objects <- function (pos = 1, pattern, order.by,
decreasing=FALSE, head=FALSE, n=5) {
napply <- function(names, fn) sapply(names, function(x)
fn(get(x, pos = pos)))
names <- ls(pos = pos, pattern = pattern)
obj.class <- napply(names, function(x) as.character(class(x))[1])
obj.mode <- napply(names, mode)
obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
@cboettig
cboettig / memcheck.sh
Created September 7, 2011 22:29
echo free memory usage with date
#!/bin/sh
echo $(date +"%y-%m-%d-%H:%M:%S") $(free -m | sed -n '2p' | awk '{print $3}') MB >> mem.log
@cboettig
cboettig / flush-swap.sh
Created October 12, 2011 15:45
flush swap
# Commands to flush swap.
# Don't try this if swap in use exceeds memory available.
sudo swapoff -a
sudo swapon -a
# sync writes things in memory to hard disk (kernel) for stability.
sync
@cboettig
cboettig / parallel_R.R
Created November 11, 2011 00:00
cluster parallelization for R scripts
library(Rmpi)
## load the packages we'll need
RLIBS="~/R/x86_64-redhat-linux-gnu-library/2.13"
.libPaths(c(RLIBS, .libPaths()))
### Direct RMPI way:
mpi.spawn.Rslaves(nslaves=15)
slavefn <- function() { print(paste("Hello from", foldNumber)) }
mpi.bcast.cmd(foldNumber <- mpi.comm.rank())
@cboettig
cboettig / run.sh
Created November 11, 2011 00:02
queue commit files
#!/bin/bash
## Use the current working directory
#$ -cwd
## use bash commands
#$ -S /bin/bash
## combine error and output files
#$ -j y
## Parallel for openmp:
#$ -pe threaded 16
## Launch parallel mpi threads
@cboettig
cboettig / gist:1387759
Created November 23, 2011 02:41
Sweave template using pretty syntax highlighting and elsarticle class
\documentclass{elsarticle}
%% Redefines the elsarticle footer
\makeatletter
\def\ps@pprintTitle{%
\let\@oddhead\@empty
\let\@evenhead\@empty
\def\@oddfoot{\it \hfill\today}%
\let\@evenfoot\@oddfoot}
\makeatother
@cboettig
cboettig / Makefile
Created December 12, 2011 20:57
Makefile for Sweave documents to use nicer syntax highlighting
RSCRIPT = Rscript
pdfclean:
rm -f *.pdf
clean:
rm -f index.html
rm -f *.tex *.bbl *.blg *.aux *.out *.log *.spl
@cboettig
cboettig / Makefile
Created December 13, 2011 05:49
knitr makefile
## Makefile to use knitr in the vignette
RSCRIPT = Rscript
all: rfishbase.pdf
rfishbase.pdf: rfishbase/rfishbase.Rnw
cp -f rfishbase/rfishbase.Rnw .
$(RSCRIPT) --vanilla -e "require(knitr); knit( 'rfishbase.Rnw');"
#Compile in latex, but keep aux file till we make bibliography
$(RSCRIPT) -e "tools::texi2dvi( 'rfishbase.tex', pdf = TRUE, clean = FALSE )"
@cboettig
cboettig / nersc_install.sh
Created January 7, 2012 00:57
Install wrightscape on NERSC systems
module swap pgi intel
module swap openmpi openmpi-intel
module load gsl
module load R/2.12.1
rm wrightscape/config*
cp wrightscape/src/nersc_makevars wrightscape/src/Makevars
R CMD INSTALL wrightscape