Skip to content

Instantly share code, notes, and snippets.

View romainfrancois's full-sized avatar
🎉
tada⬢science ⬡⬡ ex(Posit/RStudio, ThinkR, Mango Solutions)

Romain François romainfrancois

🎉
tada⬢science ⬡⬡ ex(Posit/RStudio, ThinkR, Mango Solutions)
View GitHub Profile
@romainfrancois
romainfrancois / gist:6119995
Created July 31, 2013 07:20
Two workarounds to get access to the c level connections api from c++.
#include <Rcpp.h>
// thanks to Simon for the macro magic
// problem #1: R internals uses "private" and "class" as
// member names. So a C++ compiler is confused
// when we include Connections.h
// this workaround uses the preprocessor to rename
// class as class_name and private as private_ptr
#define class class_name
#define private private_ptr
@romainfrancois
romainfrancois / README.md
Last active December 23, 2015 17:59
USE_RINTERNALS and Rcpp
$ R CMD SHLIB size.c
$ Rscript -e "dyn.load('size.so'); siz <- function(.) .Call( 'siz', .) ; siz(1:10); siz(siz); siz(letters) "
[1] 88
[1] 600
[1] 1496
@romainfrancois
romainfrancois / mod.cpp
Created September 25, 2013 06:42
Problem exposing inherited member function of derived class to R through RCPP_MODULE
#include <Rcpp.h>
using namespace Rcpp;
class B {
public:
B (SEXP b);
};
template <class T> class A {
public:
@romainfrancois
romainfrancois / NA_Proxy.h
Last active December 23, 2015 21:59
A new approach of NA handling
// Copyright (C) 2013 Romain Francois
//
// This file is part of Rcpp11.
//
// Rcpp11 is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp11 is distributed in the hope that it will be useful, but
@romainfrancois
romainfrancois / dots.R
Last active December 24, 2015 09:59
Passing down unevaluated parameters
require(Rcpp)
sourceCpp( "dots.cpp" )
x <- 1
f <- function(...) {
x <- 2
g(..., b = x)
}
g <- function(...) {
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector rowApply0(NumericMatrix& x, const Function& FUN)
{
int n = x.nrow();
NumericVector result = no_init(n);
for (int r = 0; r < n; r++) {
@romainfrancois
romainfrancois / gist:6981372
Last active December 25, 2015 13:09
Reading the internal function table
#include <Rcpp.h>
using namespace Rcpp;
typedef SEXP (*CCODE)(SEXP, SEXP, SEXP, SEXP);
/* Information for Deparsing Expressions */
typedef enum {
PP_INVALID = 0,
PP_ASSIGN = 1,
PP_ASSIGN2 = 2,
@romainfrancois
romainfrancois / biglist.cpp
Created October 23, 2013 12:15
create lists with more than 20 elements
#include <Rcpp.h>
using namespace Rcpp ;
template <typename T>
inline void set_item_impl( List& target, int i, const T& obj, CharacterVector& names, traits::true_type ){
target[i] = obj.object ;
names[i] = obj.name ;
}
template <typename T>
@romainfrancois
romainfrancois / gist:7117691
Last active April 12, 2020 17:22
create list with more than 20 elements (different syntax)
#include <Rcpp.h>
using namespace Rcpp ;
template <typename T>
inline void set_item_impl( List& target, int i, const T& obj, CharacterVector& names, traits::true_type ){
target[i] = obj.object ;
names[i] = obj.name ;
}
template <typename T>
@romainfrancois
romainfrancois / output.txt
Created October 24, 2013 16:25
using SETLENGTH to fool R about the size of a vector
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>
x = <0x1038ff8a0>, dataptr = <0x1038ff8c8>