[ESS] ESS with inline package (for R) and indenting C/C++ code

Rodney Sparapani rsparapa at mcw.edu
Fri Nov 2 18:13:51 CET 2012

On 11/02/2012 09:55 AM, Ramon Diaz-Uriarte wrote:
> Dear All,
> I am using the inline package for R, and I'd like indentation to magically
> work on C/C++ code fragments.
> For example, if I type by hand the following fragment (from question 2.2
> in the Rccp-FAQ.Rnw file, by Eddelbuettel and François)
> fx<- cxxfunction(signature( x = "numeric" ),
>      'NumericVector xx(x);
>       return wrap( std::accumulate( xx.begin(), xx.end(), 0.0));',
>      plugin = "Rcpp")
> the body of the C++ code itself is not indented automagically, and when
> typing the second line, I am taken to column 0 (and tab will do
> nothing). Is it possible to avoid having to manually enter whitespace?
> Thanks,
> R.

It's pretty much hopeless within a single .R file.  I suppose that
you could consider something like mmm-mode.  What I do is I put my
C++ code in .hxx and .cxx files so that they render correctly.
And then I "source" it via...

cat.file <- function(file) {
   # cat.file returns the contents of a file as a string

   content <- NULL
   f1 <- file.info(file)

   if(!is.na(f1$size) & !f1$isdir & f1$size>0) {
     f1 <- file(file, open="r")

     if(isOpen(f1)) {
       content <- readLines(f1)



cat.char <- function(char) {
   # arrays of strings are very annoying
   # cat.char converts an array of strings into one long string with 

   cum <- NULL

   for(i in 1:length(char)) {
     if(i==1) cum <- sprintf("%s\n", char[1])
     else cum <- sprintf("%s%s\n", cum, char[i])


So, I end up with something like:
fx <- cxxfunction(signature( x = "numeric" ),
         plugin = "Rcpp")

Rodney Sparapani, PhD  Center for Patient Care and Outcomes Research
Sr. Biostatistician               http://www.mcw.edu/pcor
4 wheels good, 2 wheels better!   Medical College of Wisconsin (MCW)
WWLD?:  What Would Lombardi Do?   Milwaukee, WI, USA

More information about the ESS-help mailing list