Skip to content

Instantly share code, notes, and snippets.

@jjallaire
jjallaire / justgage.R
Created April 6, 2014 14:08
justgage
#' @export
justgage <- function(title, value, min, max,
label = NULL, width = 200, height = 160) {
structure(class = "justgage", list(
title = title,
label = label,
value = value,
min = min,
max = max,
@jjallaire
jjallaire / Presentation.Rmd
Created August 1, 2014 10:29
revealjs_presentation with self_contained: false
---
title: "Untitled"
output:
revealjs_presentation:
theme: serif
self_contained: false
incremental: true
transition: none
pandoc_args: ["--css", "style.css"]
---
@jjallaire
jjallaire / dygraphs.Rmd
Created October 28, 2014 11:49
htmlwidgets and runtime: shiny
---
title: "Dygraphs in R Markdown"
output: html_document
runtime: shiny
---
```{r}
library(dygraphs)
hw <- HoltWinters(ldeaths)
@jjallaire
jjallaire / uniform.cpp
Created December 28, 2014 20:46
Rcpp modules with sourceCpp
#include <Rcpp.h>
using namespace Rcpp;
using namespace Rcpp;
class Uniform {
public:
Uniform(double min_, double max_) : min(min_), max(max_) {}
NumericVector draw(int n) const {
RNGScope scope;
return runif( n, min, max );
@jjallaire
jjallaire / gist:ded5279b9c552ece9917
Created February 4, 2015 17:22
boost function and boost bind
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::depends(BH)]]
#include <boost/bind.hpp>
#include <boost/function.hpp>
typedef boost::function<int(int,int)> Combiner;
int add(int x, int y) {
@jjallaire
jjallaire / gist:008622dabd5730cf74cf
Created March 26, 2015 21:58
UTF8 CharacterVector
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
CharacterVector asUTF8(CharacterVector x) {
const void *vmax = vmaxget();
CharacterVector xUTF8(x.length());
for (int i = 0; i<x.length(); i++) {
const char* utf8 = Rf_translateCharUTF8(x[i]);
xUTF8[i] = Rf_mkCharCE(utf8, CE_UTF8);
@jjallaire
jjallaire / RcppAttributes.cpp
Created October 19, 2012 19:27
Rcpp example functions using attributes
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int fibonacciCpp(const int x) {
if (x == 0) return(0);
if (x == 1) return(1);
@jjallaire
jjallaire / ArmadilloTest.cpp
Created October 24, 2012 11:59
RcppArmadillo example
// [[Rcpp::linking_to(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
List fastLmCpp(NumericVector yr, NumericMatrix Xr) {
int n = Xr.nrow(), k = Xr.ncol();
@jjallaire
jjallaire / gist:3993978
Created November 1, 2012 14:33
Rcpp engine for knitr
```{r engine='Rcpp'}
int fibonacci(const int x) {
if (x == 0) return(0);
if (x == 1) return(1);
return (fibonacci(x - 1)) + fibonacci(x - 2);
}
```
```{r engine='Rcpp', Rcpp.plugin='RcppArmadillo'}
List fastLm(NumericVector yr, NumericMatrix Xr) {
@jjallaire
jjallaire / gist:4008371
Created November 3, 2012 19:22
Setup build environment
# Setup the build environment based on the specified dependencies. Returns an
# opaque object that can be passed to .restoreEnvironment to reverse whatever
# changes that were made
.setupBuildEnvironment <- function(depends) {
# discover dependencies
buildEnv <- list()
linkingToPackages <- c("Rcpp")
for (package in depends) {