Menu

R Indentation

Help
2010-05-21
2013-01-15
  • Frank Harrell

    Frank Harrell - 2010-05-21

    I have not been able to make rkward do automatic R-style indentation in the script editor.  I would like to have this perform as with ESS under Emacs with auto-indenting when entering {, further indenting on the line after the opening {, revert to previous indent level with closing }, etc.  Thanks for any assistance.
    Frank

     
  • Thomas Friedrichsmeier

    Hi,

    Personally, I am not a fan of automatic indentation, so maybe I'm missing something. However, you can select an indentation mode using Edit->Tools Move->Indentation (probably C-style comes closest). You can set the default indentation mode using (open a script window, first) Settings->Configure Editor->Editing->Indentation.

    Regards
    Thomas

     
  • Frank Harrell

    Frank Harrell - 2010-05-21

    Thanks for your reply Thomas.  C style goes part of the way, but it does not further indent the next line after a line containing {.
    I find that the ESS indenting style makes for more readable R code.
    Frank

     
  • Thomas Friedrichsmeier

    I'm not sure I understand what you mean. When I type

    f <- function () {
    

    using C style indentation, the next line will be (further) indented automatically. Something that does not work is this example:

    f <- function () { something
    

    Is this what you mean? (But then, this coding style does not really make for readable code at all in my opinion).

     
  • Frank Harrell

    Frank Harrell - 2010-05-22

    Here's an example:

    if(1==2)
    {
        y <- 1
    }

    is what appears.  I would like the { indented as per a popular R formatting style:

    if(1==2)
        {
            y <- 1
        }

    Having function arguments automatically indented in continuation lines would also be nice:

    lm(y ~ x,
         data=d)

    Frank

     
  • Thomas Friedrichsmeier

    Hi again,

    first, for clarification, here are your examples in "code"-tags, to make them display correctly in this forum:

    if(1==2)
    {
        y <- 1
    }
    
    if(1==2)
        {
            y <- 1
        }
    
    lm(y ~ x,
         data=d)
    

    Now, I guess tastes differ. Personally, I find that style rather appalling ;-). But if this matters to you, and you are willing to invest a bit of time, you can write an indentation script yourself. See http://dhaumann.blogspot.com/2007/07/kate-scripting-indentation.html and http://dhaumann.blogspot.com/2007/07/kate-more-on-indentation-scripting.html . Consider posting your results to this forum or to rkward-devel@lists.sourceforge.net, then we can include this in a future release.

    Regards
    Thomas

     
  • Frank Harrell

    Frank Harrell - 2010-05-22

    Thanks Thomas - I will look into this in the future.  This default style in ESS is quite nice in the view of many R programmers.