Skip to content

Instantly share code, notes, and snippets.

View zkamvar's full-sized avatar
🍄
On Sabbatical

Zhian N. Kamvar zkamvar

🍄
On Sabbatical
View GitHub Profile
library("devtools") # with_lib(), install_github()
tmp_lib <- file.path(tempdir(), ".lib")
dir.create(tmp_lib)
cran_pkg <- function(pkg, tmp_lib = tmp_lib){
## upgrade or downgrade a package here
## following example is upgrading to bleeding edge devel version
with_lib(tmp_lib, install.packages(pkg, repos = "http://cran.rstudio.com"))
## but this could also be a downgrade to the CRAN/released version!
@zkamvar
zkamvar / rdevdocker.md
Last active August 29, 2015 14:20
r-devel docker to test poppr

Notes for working with docker to test poppr on R-devel

I recently got an email from Kurt Hornik from CRAN stating that a single line of code was causing poppr to fail in R-devel. Since installing R-devel is a pain on OSX, I decided to use a docker container. Ted Hart has a good blog on it here.

On mac, I followed the instructions to install docker starting here.

I ran this once and have never had to run it again:

@zkamvar
zkamvar / power_mlg.R
Last active August 29, 2015 14:21
Playing with the power law and multilocus genotypes
plot_mlg_stats <- function(x, type = "p", palette = rainbow, ...){
layout(matrix(1:6, nrow = 2))
if (is.list(x)){
minmax <- matrix(numeric(0), nrow = 2, ncol = 8,
dimnames = list(c("min", "max"),
rownames(x[[1]])))
minmax["min", ] <- apply(vapply(x, function(i) apply(i, 1, min), numeric(8)), 1, min)
minmax["max", ] <- apply(vapply(x, function(i) apply(i, 1, max), numeric(8)), 1, max)
pal <- match.fun(palette)
cols <- pal(length(x))
@zkamvar
zkamvar / resort_microsatellites.R
Created August 6, 2015 15:42
Place microsatellites in the correct order for genind objects.
resort_microsat <- function(x){
# Gather all alleles, convert to numeric, and reorder
alls <- adegenet::alleles(x)
alln <- lapply(alls, as.numeric)
alln <- lapply(alln, order)
# Loop over the names and paste together the locus name and the sorted alleles.
alls <- lapply(names(alls), function(i) paste(i, alls[[i]][ alln[[i]] ], sep = "."))
# Convert to a vector an match the name to the column names of the data matrix.
@zkamvar
zkamvar / lazierLoad.R
Last active September 11, 2015 16:22
Does a lazy load across all files when provided a knitr cache directory.
#' Performs lazy load on a directory
#'
#' @param path a filepath containing the necessary files for lazy loading
#' @return NULL
#'
#' @details This function will go into a directory, search for all the files
#' that seem like they can be lazily loaded and attempt to load them.
#'
lazierLoad <- function(path){
files <- dir(path)
@zkamvar
zkamvar / poppr-check-2015-11-29.md
Created November 29, 2015 21:04
poppr R CMD check with address sanitizer 2015-11-29
@zkamvar
zkamvar / 2015_05_19_git2r.md
Last active December 5, 2015 01:32
Updating forks with git2r

Motivation

I am a contributor to NESCent's Population Genetics in R repository. The idea behind this repository is that people would be able to construct short vignettes for genetic analysis in R that can be displayed on the Population Genetics Info Website. Of course, the submissions would need to be pull requests from other users that would have forked the repository. For beginners, updating a fork is not a simple task. It requires that you fetch the upstream branch and then merge that branch into your head.

One problem: neither the OSX nor Windows GUIs have the ability to synch a fork from upstream, meaning that the contributors would have to know some command line options. This isn't a terrible thing all in all, because OSX has a Terminal app and users can access the shell from the Windows GUI. I recently attempted to test things on a w

@zkamvar
zkamvar / columncombiner.r
Created March 28, 2013 23:36
A function that will print out a function for OpenOffice spreadsheet or excel which can be used to concatenate cells with a common separator
################################################################################
# A by-column concatenator for excel through R.
#
# Arguments:
# row - indicate the row you wish to concatenate.
# cell - the first cell containing the information you want to concatenate.
# lastcell - the last cell containing the information you want to concatenate.
#
# Usage: combine_excel(row = 1, cell = "A", lastcell = "D")
#
@zkamvar
zkamvar / update_fork_git2r.R
Created December 6, 2015 18:56
Script for keeping a fork up to date with git2r
# 2015-12-06 10:55:45 PST ----------------------------------
# This script will allow you to update your fork of the
# NESCent popgenInfo page without having to interact with
# the command line. Note: you do not need to install git
# for this particular script to work, but it is necessary
# to use pull/commit/push through Rstudio.
library("git2r")
# Do this once and then open the Rproject file ------------
# clone(url = "",
@zkamvar
zkamvar / getfile.r
Last active December 16, 2015 11:29
Function from the package [poppr](https://github.com/poppr/poppr) that serves as a wrapper for `file.choose()`, `file.path()`, and `list.files()`. It will allow the user to grab multiple files for import into R with their gui.
#==============================================================================#
# Usage:
# x <- getfile() # for a single file.
# x <- getfile(multi = TRUE) # for multiple files
# x <- getfile(multi = TRUE, pattern = "^.+?csv$") # grabs all *.csv files.
# setwd(x$path)
# read.csv(x$files)
#==============================================================================#
getfile <- function(multi=FALSE, pattern=NULL, combine=TRUE){