Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Created February 19, 2014 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainfrancois/9094816 to your computer and use it in GitHub Desktop.
Save romainfrancois/9094816 to your computer and use it in GitHub Desktop.
calling C function directly (a la julia)
ccall <- function(fun, ..., depends = character(), plugins = character(), verbose = FALSE ){
dots <- list(...)
if( length(dots) ){
code <- sprintf( '
SEXP fun(%s){
return wrap( %s( %s ) ) ;
}',
paste( sprintf( "SEXP arg%d", seq_along(dots)), collapse = ", " ),
fun,
paste( sprintf( "Rcpp::internal::converter(arg%d)", seq_along(dots) ), collapse = ", " )
)
f <- cppFunction(code, depends = depends, plugins = plugins, verbose = verbose)
do.call(f, dots)
} else {
evalCpp( sprintf( "%s()", fun ), depends = depends, plugins = plugins)
}
}
# example:
ccall( "Rf_length", 1:10 )
@romainfrancois
Copy link
Author

This is just a convenience. It generates some code and compile it. Not quite what julia does. and see the wrap call, it is limited to what can be wraped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment