parseText: Parse a character string as if it was a command entered at...

Description Usage Arguments Value Note Author(s) See Also Examples

View source: R/parseText.R

Description

Parse R instructions provided as a string and return the expression if it is correct, or an object of class 'try-error' if it is an incorrect code, or NA if the (last) instruction is incomplete.

Usage

1
parseText(text, firstline = 1, srcfilename = NULL, encoding = "unknown")

Arguments

text

the character string vector to parse into an R expression.

firstline

the index of first line being parsed in the file. If this is larger than 1, empty lines are added in front of text in order to match the correct position in the file.

srcfilename

a character string with the name of the source file.

encoding

encoding of text, as in parse.

Value

Returns an expression with the parsed code or NA if the last instruction is correct but incomplete, or an object of class'try-error' with the error message if the code is incorrect.

Note

On the contrary to parse(), parseText() recovers from incorrect code and also detects incomplete code. It is also easier to use in case you pass a character string to it, because you don't have to name the argument explicitly (text = ....).

Author(s)

Philippe Grosjean ([email protected])

See Also

captureAll, sourceClipboard, parse

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
parseText('1+1')
parseText('1+1; log(10)')
parseText(c('1+1', 'log(10)'))

## Incomplete instruction
parseText('log(')

## Incomplete strings
parseText('text <- "some string')
parseText("text <- 'some string")

## Incomplete names (don't write backtick quoted names on several lines!)
## ...but just in case
parseText('`myvar')

## Wrong instruction
parseText('log)')

svMisc documentation built on May 29, 2017, 12:18 p.m.