Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

r - How do I substitute symbols in a language object?

Suppose I have the following language object:

lang <- quote( f(x=a) )

and I want to substitute in 1 for a. How can I do this?

I would expect substitute to do what I want, but

substitute(lang, list(a=1))

just returns lang, while

substitute(f(x=a), list(a=1))

does in fact do what I expect.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use do.call:

do.call(substitute, list(lang, list(a=1)))

By using do.call, we force evaluation of the name `lang` to its actual underlying value, f(x=a). Then substitution is performed on f(x=a), rather than the name `lang`.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...