Wolfgang Raffelsberger
2009-Sep-24 12:17 UTC
[R] how to make a function recognize the name of an object/vector given as argument
Dear guRus,
I'd like to learn how to make a function recognize the name of an
object/vector given as argument
If I have :
testFun <- function(x,y) plot(x,y, main=paste("plot
of",names(x),"and",names(y)) ) # this just a simple example ...
a1 <- 5:8
b1 <- 9:6
testFun(a1,b1)
# Returns the plot, but not the names of the objects/vectors given as
arguments,
# but since 'names()' refers to the elements INSIDE the object/vector I
don't get what I'm looking for ...
# In fact, I (also) would like to know that actually "a1" and
"b1" were
given as arguments to my function.
# As in the example, this could be useful for (sub-)titles of graphs etc...
# Is there a way to get this kind of information ?
#For compleness:
> sessionInfo()
R version 2.9.1 (2009-06-26)
i386-pc-mingw32
locale:
LC_COLLATE=French_France.1252;LC_CTYPE=French_France.1252;LC_MONETARY=French_France.1252;LC_NUMERIC=C;LC_TIME=French_France.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Thank's in advance,
Wolfgang
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Wolfgang Raffelsberger, PhD
Laboratoire de BioInformatique et G?nomique Int?gratives
CNRS UMR7104, IGBMC,
1 rue Laurent Fries, 67404 Illkirch Strasbourg, France
Tel (+33) 388 65 3300 Fax (+33) 388 65 3276
wolfgang.raffelsberger (at) igbmc.fr
baptiste auguie
2009-Sep-24 16:48 UTC
[R] how to make a function recognize the name of an object/vector given as argument
Try this,
testFun <- function(x,y) plot(x,y, main=paste("plot
of",deparse(substitute(x)),"and", deparse(substitute(y))) )
a1 <- 5:8
b1 <- 9:6
testFun(a1,b1)
?deparse
HTH,
baptiste
2009/9/24 Wolfgang Raffelsberger <wraff at
igbmc.fr>:> Dear guRus,
> I'd like to learn how to make a function recognize the name of an
> object/vector given as argument
>
> If I have :
>
> testFun <- function(x,y) plot(x,y, main=paste("plot
> of",names(x),"and",names(y)) ) ? # this just a simple
example ...
> a1 <- 5:8
> b1 <- 9:6
>
> testFun(a1,b1)
> # Returns the plot, but not the names of the objects/vectors given as
> arguments,
> # but since 'names()' refers to the elements INSIDE the
object/vector I
> don't get what I'm looking for ...
> # In fact, I (also) would like to know that actually "a1" and
"b1" were
> given as arguments to my function.
> # As in the example, this could be useful for (sub-)titles of graphs etc...
> # Is there a way to get this kind of information ?
>
> #For compleness:
>> sessionInfo()
> R version 2.9.1 (2009-06-26)
> i386-pc-mingw32
>
> locale:
>
LC_COLLATE=French_France.1252;LC_CTYPE=French_France.1252;LC_MONETARY=French_France.1252;LC_NUMERIC=C;LC_TIME=French_France.1252
>
> attached base packages:
> [1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base
>
> Thank's in advance,
> Wolfgang
>
>
> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
> Wolfgang Raffelsberger, PhD
> Laboratoire de BioInformatique et G?nomique Int?gratives
> CNRS UMR7104, IGBMC, ?1 rue Laurent Fries, ?67404 Illkirch ?Strasbourg,
> ?France
> Tel (+33) 388 65 3300 ? ? ? ? Fax (+33) 388 65 3276
> wolfgang.raffelsberger (at) igbmc.fr
>
> ______________________________________________
> 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.
>