Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

In the following code, I want to print the output of fn(a = b) as:

"a = b" and fn(a = gn(b), 3~b~a, dd, e = 2 + f, h = hn(jn(cdf))) to be list("a=gn(b)", "3~b~a", "dd", "e=2+f", "h= hn(jn(cdf)))"

fn = function(...) {

  # print the content of ... without evaluation?

}

The closest I got was this:

fn = function(...) {

   res = rlang::enexprs(...)

   paste0(names(res), ifelse(names(res)=="",names(res) , "=") , sapply(res, capture.output))

}

1 Answer

0 votes
by (107k points)

For achieving that you can simply use match.call() :

fn = function(...) {

  temp <- as.list(match.call())[-1]

  as.list(sub('^=', '', paste0(names(temp), '=', temp)))

}

fn(a = b)

#[[1]]

#[1] "a=b"

fn(a = gn(b), 3~b~a, dd, e = 2 + f, h = hn(jn(cdf)))

#[[1]]

#[1] "a=gn(b)"

#[[2]]

#[1] "3 ~ b ~ a"

#[[3]]

#[1] "dd"

#[[4]]

#[1] "e=2 + f"

#[[5]]

#[1] "h=hn(jn(cdf))"

If you are a beginner and want to know more about R then do check out the R programming course

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...