This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (.Platform$OS.type == "unix") { | |
setwd("~/your/path/to/the/working_directory/in/OSX") | |
} else { | |
setwd("your/path/to/the/working_directory/in/WINDOWS") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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%.*}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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", |