Skip to content

Instantly share code, notes, and snippets.

@kevinushey
kevinushey / server.R
Created September 4, 2014 23:53
Input slider has some bugs when used through uiOutput
library(shiny)
## Example
# shinyServer(function(input, output) {
# output$value <- renderPrint({ input$sliderId })
# })
## uiOutput
shinyServer(function(input, output) {
output$slider <- renderUI({
@kevinushey
kevinushey / strings_with_rcpp.Rmd
Last active December 10, 2015 20:28
Handling Strings with Rcpp
---
title: Handling Strings with Rcpp
author: Kevin Ushey
license: GPL (>= 2)
tags: string vector
summary: Demonstrates how one might handle a vector of strings with `Rcpp`,
in addition to returning output.
---
This is a quick example of how you might use Rcpp to send and receive R
---
title: Using Boost's foreach macro
author: Kevin Ushey
license: GPL (>= 2)
tags: basics boost
summary: Boost's BOOST_FOREACH can enable a more functional programming style.
---
Boost provides a macro, `BOOST_FOREACH`, that allows us to easily iterate
over elements in a container, similar to what we might
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
StringVector check( StringVector x ) {
Rcout << "The type of x is: " << ::TYPEOF( x ) << std::endl;
Rcout << "The type of x(0) is: " << ::TYPEOF( x(0) ) << std::endl;
Rcout << "The integer associated with STRSXPs is: " << STRSXP << std::endl;
Rcout << "The integer associated with CHARSXPs is: " << CHARSXP << std::endl;
Rcout << "Is x a string?: " << ::Rf_isString(x) << std::endl;
@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.
@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 / 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
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 / 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;
@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;