Created
February 19, 2014 15:51
-
-
Save romainfrancois/9094816 to your computer and use it in GitHub Desktop.
calling C function directly (a la julia)
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
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 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 bewrap
ed