Difference between revisions of "Parser.R"

From Organic Design wiki
m
m
 
Line 1: Line 1:
# {{R}}
+
<source lang="r">
 
# -------------------- Examples demonstrating the R parser -------------------- #
 
# -------------------- Examples demonstrating the R parser -------------------- #
 
# Std input
 
# Std input
Line 45: Line 45:
 
eval(parse(text="x < b & y >= 3"))
 
eval(parse(text="x < b & y >= 3"))
 
eval(parse(text=x))
 
eval(parse(text=x))
 +
</source>

Latest revision as of 15:49, 26 November 2019

# -------------------- Examples demonstrating the R parser -------------------- #
# Std input
# Std output & Std error
# The parser tries to evaluate the expression in the console (unless character)
 # See also p 34 part-02.pdf (Ihakas 2006 workshop)
# ----------------------------------------------------------------------------- #

# expression()  Creates or tests for objects of mode 
# eval()        Evaluate an R expression in a specified environment
# quote()       Simply returns its argument. The argument is not evaluated and can be any R expression.
# evalq         'evalq' form is equivalent to 'eval(quote(expr), ...)'.
# substitute()  Substituting and Quoting Expressions
# parse()       Returns the parsed but unevaluated expressions in a list. Each element of the list is of mode 'expression'.
# deparse()     Turn unevaluated expressions into character strings

x <- expression(1 + 0:9)
x
length(x)
mode(x)
eval(x)

x <-expression({a <- 1:10; a^2})
eval(x)

quote(x<1 & y>=10)
quote(x)
evalq(x)
eval(quote(x)) # identical 

# substitute allows env auxillary information
substitute(expression(a + b), list(a = 1))

example(parse)
parse(text="x < b & y >= 3")
x <- "x < b & y >= 3"
parse(text=x)



eval(expression("x < b & y >= 3"))
eval(expression(x))

# identical (fails if x, b, and y cannot be found)
eval(parse(text="x < b & y >= 3"))
eval(parse(text=x))