Say I have a function my.function
taking x
, a numerical vector, as its only argument. I need to decompose or parse the string from match.call()$x
in such a way that I can identify:
- the vector's name and label if any
- the structure it's in, if any (dataframe, list, and so on.)
- the structure this latest structure is in... and so on.
In other words, I need to deduct the hierarchy of the data from the function call. For example, say the function call is
> my.function(iris$Species)
strsplit
or regular expressions will tell us that Species is an atomic vector, contained in a dataframe called iris. (is.vector
, is.data.frame
and others could be used for validating this). The thing gets more complicated though as structures are part of larger structures, and as the syntaxically diverse ways to extract data from structures grow.
To illustrate, imagine that instead of iris$Species, the user uses this (after putting iris
in a list, for whichever reason:
> my.function(my.list["iris"][,5])
> my.function(my.list[[2]]$iris[,"Species"]
In order to achieve what I want, I would need to come up with a certain number of regular expressions. Now my question is: before working those regexps, am I overlooking some existing function or an alternate way to deduct hierarchy from the function call?
After experimenting for a bit with MrFlick's solution, I found I got better results using regex. It doesn't account for all situations, but it gives me more than enough flexibility. Thought I'd share it here, might be useful to others. No garanties of course.