Skip to content

Instantly share code, notes, and snippets.

@kevinushey
kevinushey / server.R
Created September 17, 2013 21:11
shinyGridster test
library(shiny)
shinyServer( function(input, output, session) {
})
@kevinushey
kevinushey / RcppMelt.md
Last active August 2, 2016 03:13
Implementing reshape2::melt in Rcpp.

Melting with Rcpp

A common data manipulation task is that of making 'wide' data 'long': for example, using the reshape2 library,

library(reshape2)
wide_df <- data.frame( stringsAsFactors=FALSE,
@kevinushey
kevinushey / .gitignore
Last active December 21, 2015 21:39 — forked from hadley/.gitignore
.Rproj.user
.Rhistory
.RData
*.Rproj
*.html
@kevinushey
kevinushey / RcppArmadillo_row_shuffle.cpp
Created August 9, 2013 00:06
RcppArmadillo row shuffling
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::imat shuffle(arma::imat A) {
for (int i=0; i < A.n_rows; ++i) {
A.row(i) = shuffle( A.row(i), 1 );
}
return A;
@kevinushey
kevinushey / index.html
Created June 18, 2013 03:20
Scatterplot
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<script src='http://polychart.com/s/third_party/polychart2.standalone.js' type='text/javascript'></script>
<style>
.rChart {
display: block;
a <- 1
b <- 2
f <- function(x) {
call <- match.call()
len <- length( call[[2]] )
output <- x
names(output) <- sapply( call[[2]][2:len], as.character )
return(output)
}
@kevinushey
kevinushey / swap.R
Created May 15, 2013 18:43
A handy function for swapping values in a vector with other values.
swap <- function(vec, from, to) {
tmp <- to[ match(vec, from) ]
tmp[is.na(tmp)] <- vec[is.na(tmp)]
return(tmp)
}
## example
swap( 1:10, c(5, 7), c(50, 70) ) ## exchange occurrences of 5 w/ 50, 7 w/ 70
@kevinushey
kevinushey / Rcpp_wrap_and_recurse.Rmd
Last active December 15, 2015 22:28
Dynamic Wrapping and Recursion with Rcpp
---
title: Dynamic Wrapping and Recursion with Rcpp
author: Kevin Ushey
license: GPL (>= 2)
tags: basics
summary: We can use parts of R's API alongside Rcpp to recurse through
lists and dynamically wrap objects as needed.
---
We can leverage small parts of the R's C API in order to
@kevinushey
kevinushey / pymat.R
Last active December 15, 2015 12:19
Python style formatting of strings
pymat <- function(string, ...) {
i <- 0
for( arg in list(...) ) {
to_replace <- paste( sep='', "\\{", i, "\\}" )
string <- gsub( to_replace, arg, string )
i <- i + 1
}
return( string )
}
@kevinushey
kevinushey / fast_factor_generation.Rmd
Last active December 14, 2015 06:49
Fast factor generation with Rcpp
---
title: Fast factor generation with Rcpp
author: Kevin Ushey
license: GPL (>= 2)
tags: factor tapply sugar
summary: We can make use of Rcpp sugar to implement a faster factor generator.
---
Recall that factors are really just integer vectors with 'levels',
i.e., character labels that get mapped to each integer in the vector.