Skip to content

Instantly share code, notes, and snippets.

View lauratboyer's full-sized avatar

Laura Tremblay-Boyer lauratboyer

  • CSIRO
  • Hobart, Australia
View GitHub Profile
## custom-color-palettes.r
## Defining custom color palettes and loading packages for others
## -------------------------------------------------------
## Author: Laura Tremblay-Boyer (lauratb@spc.int)
## Written on: August 20, 2015
## Time-stamp: <2015-08-20 10:12:30 lauratb>
require(RColorBrewer)
require(colorspace)
@lauratboyer
lauratboyer / table2pdf.r
Created September 7, 2015 20:04
Creates pdf table from matrix or dataframe --- table is cropped in mac, Ghostscript needs to be called directly for PC (not done here)
# table2pdf()
# Creates pdf table from matrix or dataframe
# Author: Laura Tremblay-Boyer, UBC Fisheries Centre
# Contact: l.boyer@fisheries.ubc.ca
# Note 1: Current version works on Mac, but can be tweaked for a PC.
# You need a working version of latex in your computer. Make sure
# latex in your R search path. Sys.getenv("PATH") will show you the value
# of your current path, see ?Sys.getenv and Details section in ?latex for more.
if(!grepl("tex",Sys.getenv("PATH"))) {
## add-continents.r
## Defines function add.continents() which adds a quick, low-level
## outline of the world's continents to an existing plot
## (i.e. no need to fuss around with special map classes,
## but the resulting outline is coarse)
## if use.ndc = TRUE, will draw continents over the entire figure
## device
## -------------------------------------------------------
## Author: Laura Tremblay-Boyer (l.boyer@fisheries.ubc.ca)
.First.sys() # loads all base packages, etc. before doing .Rprofile commands
# packages to load
#library(data.table)
# set locale to utf-8
#Sys.setlocale("LC_ALL", "en_US.UTF-8")
Sys.setlocale("LC_ALL", "fr_FR.UTF-8")
message("Locale set to UTF-8 (or not, check in Rprofile)\n -- won't work for PF project, required for GS Soproner")
@lauratboyer
lauratboyer / .emacs
Created January 26, 2015 20:42
Emacs configuration file
;;; Define load path
(add-to-list 'load-path "~/.emacs.d/")
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")
;;; Set preferred encoding to utf-8
;;; when using shell within emacs (M-x shell)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
@lauratboyer
lauratboyer / check-dev-size.r
Created July 25, 2013 19:39
Looks at current graphic device size, if it's not the specified width and height closes it and opens a new one.
check.dev.size <- function(ww,hh) {
if(dev.cur()==1){ dev.new(width=ww,height=hh)
} else {
ds <- dev.size()
if(round(ds[1],2)!=round(ww,2)
| round(ds[2],2)!=round(hh,2)) {
dev.off(); dev.new(width=ww,height=hh)} }
}
@lauratboyer
lauratboyer / getmatch.r
Created July 24, 2013 18:11
This function allows you to extract the part of the string that matches the regular expression given as input. For instance, if you have species names and want to extract the gender only, you would type: getmatch("Echinometra mathaei", "^\\w+") # Echinometra
getmatch <<- function(x,str2match,...) {
unlist(regmatches(x,regexpr(str2match,x,...))) }
@lauratboyer
lauratboyer / file-scan.r
Last active December 15, 2015 10:09
## file-scan.r ## Function to find a pattern in files with user-defined extension (.r by default), ## optional fpattern exact match to file name. ## Search directory is working directory by default but can be set with dir. ## Returns file name and approximate row matches if positive.
## file-scan.r
## Function to find a pattern in files with user-defined extension (.r by default)
## Returns file name and approximate row matches if positive.
## -------------------------------------------------------
## Author: Laura Tremblay-Boyer (l.boyer@fisheries.ubc.ca)
## Written on: March 26, 2013
## Time-stamp: <2013-03-26 16:19:28 Laura>
file.scan <- function(pattern, dir=getwd(), fpattern=NA, ftype=".r") {
@lauratboyer
lauratboyer / getGODAS.r
Last active December 15, 2015 01:59
Code to extract GODAS oceanography data from database, given that connection "channel" is defined.
#Function to extract GODAS oceanography data
#from SPC databases (adapted from Simon Hoyle)
#Gets the value from the nearest lon/lat grid cell
#lat resolution is 1/3 of a degree, lon is 1 degree
#Started: <2013-03-14 15:29:57 Laura>
#Time-stamp: <2013-03-15 14:37:55 Laura>
getGODAS <- function(tb,variable,year,month,rlat,rlon) {