evalparse: Wrapper for eval and parse text

Description Usage Arguments Details See Also Examples

View source: R/evalparse.R

Description

Takes a character/text string and evaluates it as if it was code (an R expression). parse() returns the text as if it was code. eval then runs/evaluates the code afterwards.

Usage

1

Arguments

x

A character string to be converted to code (IE parsed) and run (IE evaluated).

Details

Simplistically, the purpose of this idea is to get into the realm of code that can write code, but this is dependent on either the input parameters or the data, so not automatic. The purpose of this specific function, however, is for saving on typing: eval(parse(text = x)) VS evalparse(x).

See Also

eval, parse

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
if(runif(n = 1) >= 0.5){
  vec <- c("mickey mouse", "bambi", "herbert hoover")
} else {
  vec <- c(12.6548, 13.21549, 84.946562)
}

my_string <- sprintf(
  "as.%s(
     substring(
         trimws(vec, which = 'left')
       , first = 1
       , last  = 4
     )
   )"
  , class(vec)
  )

evalparse(my_string)

Paul-James/pjames documentation built on Aug. 9, 2019, 12:18 p.m.