Skip to content

Instantly share code, notes, and snippets.

@barryrowlingson
barryrowlingson / submitR.sh
Last active April 8, 2022 16:21
Run R scripts in SGE
#!/bin/bash
. /etc/profile
usage () {
printf "Usage: %s: [-h] -m -d -c -n name -j jobname -b Bfile -e Efile <Rfile> [Rargs]\n" $(basename $0) >&2
}
mflag=
nflag=
@barryrowlingson
barryrowlingson / convertPDF.sh
Created December 2, 2014 18:17
Convert PDF to images in a word doc
#!/bin/bash
mkdir -p pages
rm pages/image*png
convert -verbose -colorspace RGB -units PixelsPerInch -resize 1024 -geometry 1224 -interlace none -density 200 -quality 80 $1 pages/image.png
npages=`ls pages/image-*.png| wc -l`
for (( i=0 ; i<$npages ; i++ ))
do
echo '![](pages/image-'$i'.png)'
echo " "
@barryrowlingson
barryrowlingson / readit.R
Created December 5, 2014 23:21
read some data file..
readit = function(filename){
lines = readLines(filename)
whichdata = grep("^[0-9]{4}",lines)
headers = read.table(textConnection(lines[min(whichdata)-1]))
datalines = lines[whichdata]
dmat = read.table(textConnection(datalines),na.strings=c("***","****"))
names(dmat)=t(headers)[,1]
dmat
}
@barryrowlingson
barryrowlingson / mdiff
Created January 27, 2015 15:36
sequential file diff
#!/bin/bash
if [ "$#" == "0" ]; then
echo "diff [file1] [file2] ..."
echo " Does diff file1 file2 ; diff file2 file3 ; diff file3 file4 ; etc"
exit 1
fi
first="$1"
shift
@barryrowlingson
barryrowlingson / areaLL.R
Created February 19, 2015 22:21
compute surface area for lat-long grids.
areaLL <- function(r){
## cell areas in km^2
areas = area(r)
## area of non-NA in km^2
## this is the flat area
a = sum(values(area(r)*(!is.na(r))))
centre = cellFromRowCol(r, nrow(r)/2, ncol(r)/2)
## compute w/h of middle cell
a1 = areas[centre]
@barryrowlingson
barryrowlingson / smarties.R
Created March 4, 2015 12:03
Smarties colour palette
### Colours of old Smarties with lovely artificial colouring, derived from:
###
###
### "Smarties old new" by John Penton and Paul Hughes - Own work. Licensed under CC BY-SA 2.5 via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:Smarties_old_new.jpg#mediaviewer/File:Smarties_old_new.jpg
###
### image was loaded into gimp, representative parts of each Smartie were isolated, the image converted to indexed palette
### with 9 colours, the palette then saved.
##
### "Smarties" is a trademark of Nestle
##
@barryrowlingson
barryrowlingson / fixmap.R
Last active May 16, 2016 14:41
Save leaflet maps to one place and use LiveReload
'''
Maps...
Normally when using the leaflet package you do:
leaflet(data) %>% addTiles() %>% addPolygons()
and the html is saved in a new place and your web browser gets another tab appear.
After the 26th attempt to get the map right I have 25 tabs to close.
@barryrowlingson
barryrowlingson / nn.R
Created April 15, 2015 15:33
computing nearest neighbours with RANN
## lets use this nearest neighbour package:
require(RANN)
## create a village as a cluster of houses around x,y:
village <- function(x, y, n, id, sd){
## return a data frame of x, y, and id values:
data.frame(x=x+rnorm(n,0,sd),
y=y+rnorm(n,0,sd),
id=id
)
@barryrowlingson
barryrowlingson / topdf
Last active August 29, 2015 14:22
convert a bunch of docs to pdf
#!/bin/sh
for f in $* ; do
libreoffice "-env:UserInstallation=file:///tmp/LibO_Conversion" --headless --invisible --convert-to pdf "$f"
done
rm -rf /tmp/LibO_Conversion
@barryrowlingson
barryrowlingson / ggfail.R
Last active January 29, 2018 15:50 — forked from anonymous/ggfail.R
polygon clipping in ggplot
library(sp)
library(rgdal)
library(ggplot2)
library(spdep)
library(ggpolypath)
usq = data.frame(x=c(0,1,1,0),y=c(0,0,1,1))
pts1 =
structure(list(x = c(-0.380226816911908, -0.368506824640174,