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
--- | |
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 |
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
// the column-wise implementation is just as fast as colMeans, | |
// but the row-wise operation is not quite as fast as rowMeans. | |
// can I do better? | |
#include <Rcpp.h> | |
using namespace Rcpp; | |
template <class T> | |
inline double do_mean( T& x ) { |
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
--- | |
title: Make your own 'apply' functions for matrices | |
author: Kevin Ushey | |
license: GPL (>= 2) | |
tags: Rcpp apply matrix | |
summary: Clever use of sourceCpp can allow you to define your own 'apply' functions on the fly. | |
--- | |
Make your own 'apply' functions | |
========== |
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
--- | |
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 |
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
#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; |
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
--- | |
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. |
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
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 ) | |
} |
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
--- | |
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 |
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
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 |
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
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) | |
} |
OlderNewer