[R] Substitute in function calling a function

Felix Andrews felix at nfrac.org
Mon Oct 22 10:36:15 CEST 2007


I think the usual thing would be to pass substitute(x) or
deparse(substitute(x)) from the original function (fun2 in your
example).

But if you really want to, you can do
fun <- function(x) eval.parent(call("substitute", substitute(x)))


On 10/22/07, Steve Powell <steve at promente.org> wrote:
> Dear list members,
> I am writing some functions to help with printing graphs.
> If I want to return the name of a variable within a function, for instance
> to print the label for a graph, I know that I can use substitute:
> fun=function(x) substitute(x)   #plus of course some other processing
> var=1:3
> fun(var) #prints "var" as required, not "x".
>
> But if I call this function in another function
> fun2=function(y) fun(y) #plus of course some other processing
> fun2(var) #prints "y" when what I wanted was "var"
>
> I have tried lots of tricks using eval and expression but can't seem to get
> it right. Any ideas?
> Thanks in advance
>
> Steve Powell
>
> proMENTE social research
> research | evaluation | training & consulting
>
> Kranjčevićeva 35, 71000 Sarajevo
> mobile: +387 61 215 997 | office: +387 33 556 865 | fax: +387 33 556 866
> skype: stevepowell99
> www.promente.org
>
>
>
>
> Checked by AVG Free Edition.
>
> 19.10.2007
> 05:10
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Felix Andrews / 安福立
PhD candidate
Integrated Catchment Assessment and Management Centre
The Fenner School of Environment and Society
The Australian National University (Building 48A), ACT 0200
Beijing Bag, Locked Bag 40, Kingston ACT 2604
http://www.neurofractal.org/felix/
3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8


More information about the R-help mailing list