Skip to content

Instantly share code, notes, and snippets.

@air-drummer
air-drummer / dirname(getwd())
Last active August 29, 2015 14:09
Go up one folder to save output (especially, large data)
up_one_folder_because_of_git <- dirname(getwd())
data_folder <- file.path(up_one_folder_because_of_git, "data")
dir.create(data_folder, showWarnings = FALSE)
write.table(BIG_TABLE,
file = file.path(data_folder,
paste0(BIG_TABLE_NAME, ".csv")),
sep = ",", row.names = F)
source_https <- function(url, ...) {
## Function for sourcing individual R scripts from GitHub
## Author: Tony Breyal
## Contributions: Kay Cichini
## Original URL: http://tonybreyal.wordpress.com/2011/11/24/source_https-sourcing-an-r-script-from-github/
## Load required package
require(RCurl)
## Parse and evaluate each .R script
sapply(c(url, ...), function(u) {
eval(parse(text = getURL(u, followlocation = TRUE,
@air-drummer
air-drummer / load.packages
Last active August 29, 2015 14:09
Loading packages in R in one shot
#Loading packages in R in one shot
load.packages <- function(..., install = TRUE){
reqFun <- function(pack) {
if(!suppressWarnings(suppressMessages(require(pack, character.only = TRUE)))) {
message(paste0("unable to load package ", pack,
": attempting to download & then load"))
install.packages(pack)
require(pack, character.only = TRUE)
}
@air-drummer
air-drummer / valid_path_mac_win
Last active August 29, 2015 14:08
Making path to working directory valid in different platforms (OS X, Windows, etc.) in R
if (.Platform$OS.type == "unix") {
setwd("~/your/path/to/the/working_directory/in/OSX")
} else {
setwd("your/path/to/the/working_directory/in/WINDOWS")
}
@air-drummer
air-drummer / change_doc_output
Created October 31, 2014 04:36
pandoc conversion in R
#assuming we have a "example.rmd" file in our working directory
library(knitr)
knit2html("example.rmd")
# installing/loading the package:
if(!require(installr)) { install.packages("installr"); require(installr)} #load / install+load installr
# Installing pandoc
install.pandoc()
FILE <- "example"
@air-drummer
air-drummer / latexmk code
Last active August 29, 2015 14:08
latexmk engine + knitr (Mac OS)
#There isn't a built-in engine for knitr, but it's easy to make one.
#The engine itself is just a shell script. Here's my belt-and-braces version that believes you are on a unix machine
#but doubts that your paths are set up properly:
#!/bin/bash
export PATH=$PATH:/usr/texbin:/usr/local/bin
if (Rscript -e "library(knitr); knit('$1')") then
latexmk -pdf "${1%.*}"
@air-drummer
air-drummer / run.in.dir
Last active August 29, 2015 14:08
Running chunks of R code in an arbitrary working directory
#Hadley: I use the following function to run chunks of R code in an arbitrary working directory.
run.in.dir <- function(dir, code) {
cur <- getwd()
setwd(dir)
on.exit(setwd(cur))
force(code)
}
@air-drummer
air-drummer / read.csv.url
Last active August 29, 2015 14:08
Getting csv data.frame from an "https" source in R (e.g. GitHub)
# if you are OK if a data.frame is produced and you don't want the function to install anything:
read.csv.url <- function(url, method = "curl", ...) {
tmpFile <- tempfile()
download.file(url, destfile = tmpFile, method = method)
url.data <- read.csv(tmpFile, ...)
return(url.data)
}
#' Modified version of the ggplot2 plotmatrix function that accepts additional
#' variables for aesthetic mapping.
#'
#' example
#' data(iris)
#' iris.vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
#' ggpairs(data = iris, facet.vars = iris.vars,
#' mapping = aes(color = Species, shape = Species))
ggpairs <- function (data, facet.vars = colnames(data), facet.scale = "free",