Description Usage Arguments Value Note See Also Examples
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.
1 2 3 |
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 |
srcfilename |
A character string with the name of the source file. |
encoding |
Encoding of 'text“, as in |
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.
On the contrary to parse()
, parse_text()
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 = ...
).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | parse_text("1 + 1")
parse_text("1 + 1; log(10)")
parse_text(c("1 + 1", "log(10)"))
# Incomplete instruction
parse_text("log(")
# Incomplete strings
parse_text("text <- \"some string")
parse_text("text <- 'some string")
# Incomplete names (don't write backtick quoted names on several lines!)
# ...but just in case
parse_text("`myvar")
# Incorrect expression
parse_text("log)")
|
Attaching package: 'svMisc'
The following object is masked from 'package:utils':
?
expression(1 + 1)
expression(1 + 1, log(10))
expression(1 + 1, log(10))
[1] NA
[1] NA
[1] NA
[1] NA
[1] "1:4: unexpected ')'\n1: log)\n ^"
attr(,"class")
[1] "try-error"
attr(,"error")
<simpleError in "log)": 1:4: unexpected ')'
1: log)
^>
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.