Learn R Programming

formatR (version 0.2-4)

parse.tidy: Modified versions of parse() and deparse()

Description

These two functions parse and deparse the masked source code.

Usage

parse.tidy(text, ...)

deparse.tidy(expr, ...)

Arguments

text
the source code as a character string to be passed to tidy.source
...
for parse.tidy: other arguments to be passed to tidy.source; for deparse.tidy: arguments to
expr
the unevaluated expressions (ideally as results from parse.tidy)

Value

Details

For parse.tidy, the source code is masked to preserve comments, then this function uses parse to return the parsed but unevaluated expressions in a list.

For deparse.tidy, it uses deparse to turn the unevaluated (and masked) expressions into character strings; the masks will be removed to restore the real source code. See unmask.source.

See Also

parse, deparse, tidy.source

Examples

Run this code
src = c("# a single line of comments is preserved", 
    "1+1", "", "if(TRUE){", "x=1  # comments begin with at least 2 spaces!", 
    "}else{", "x=2;print('Oh no... ask the right bracket to go away!')}", 
    "1*3 # this comment will be dropped!", "2+2+2    # 'short comments'", 
    "lm(y~x1+x2)  ### only 'single quotes' are allowed in comments", 
    "1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line")

(expr = parse.tidy(src))

parse.tidy(src, keep.blank.line = TRUE)

cat(deparse.tidy(expr))

deparse.tidy(expr, width.cutoff = 50)

Run the code above in your browser using DataLab