Learn R Programming

formatR (version 0.2-4)

formatR: A GUI to format R code

Description

Create a GUI (via GTK+ by default) to format R code.

Usage

formatR(guiToolkit = "RGtk2")

Arguments

guiToolkit
the GUI toolkit to use

Value

  • the text widget is returned

Details

This function calls tidy.source to format R code. Spaces and indent will be added to the code automatically.

We can either open an R source file or directly write R code in the text widget. Click the ``convert'' button, and the code will become tidy. See tidy.source for more details.

References

http://yihui.name/en/2010/04/formatr-farewell-to-ugly-r-code/

See Also

tidy.source

Examples

Run this code
if (interactive() && require("gWidgetsRGtk2")) {
    
    ## a GUI will show up on loading if one of the gWidgets
    ##   toolkit is present (e.g. via library(gWidgetsRGtk2))
    library(formatR)
    
    g = formatR()
    
    ## we have control over the text widget, e.g. set or get
    #   the text
    
    svalue(g) = c("# a single line of comments is preserved", 
        "1+1", "if(TRUE){", paste("x=1 ", "# inline comments!"), 
        "}else{", "x=2;print('Oh no... ask the right bracket to go away!')}", 
        "1*3 # another inline comment")
    
    ## click 'Convert' now, and see
    
    cat(svalue(g), sep = "")  # get its value
    
    ## tcl/tk interface: need gWidgetstcltk package
    formatR("tcltk")
    
}

Run the code above in your browser using DataLab