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

Ramon Diaz-Uriarte rdiaz02 at gmail.com
Sat Nov 3 00:08:29 CET 2012



Hi Rodney,


On Fri, 02 Nov 2012 12:13:51 -0500,Rodney Sparapani <rsparapa at mcw.edu> wrote:
> 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

I'll look into mmm-mode.


> 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)

>        close(f1)
>      }
>    }

>    return(cat.char(content))
> }

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

>    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])
>    }

>    return(cum)
> }

> So, I end up with something like:
> fx <- cxxfunction(signature( x = "numeric" ),
> 	includes=cat.file.R("fx.hxx"),
> 	body=cat.file.R("fx.cxx"),
>          plugin = "Rcpp")


Thanks for the suggestion and the two helper functions!


Best,


R.



> -- 
> 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
-- 
Ramon Diaz-Uriarte
Department of Biochemistry, Lab B-25
Facultad de Medicina 
Universidad Autónoma de Madrid 
Arzobispo Morcillo, 4
28029 Madrid
Spain

Phone: +34-91-497-2412

Email: rdiaz02 at gmail.com
       ramon.diaz at iib.uam.es

http://ligarto.org/rdiaz



More information about the ESS-help mailing list