[R] History pruning

Duncan Murdoch murdoch at stats.uwo.ca
Thu Jul 31 21:12:03 CEST 2008


On 7/31/2008 2:08 PM, Ken Williams wrote:
> 
> 
> On 7/31/08 11:01 AM, "hadley wickham" <h.wickham at gmail.com> wrote:
> 
>> I think that would be a very hard task -
> 
> Well, at least medium-hard.  But I think significant automatic steps could
> be made, and then a human can take over for the last few steps.  That's why
> I was enquiring about "tools" rather than a complete solution.
> 
> Does R provide facilities for introspection or interrogation of expression
> objects?  I couldn't find anything useful on first look:

You can index an expression as a list:

 > e <- expression(foo <- 5 * bar)
 >
 > e[[1]]
foo <- 5 * bar
 > str(e[[1]])
  language foo <- 5 * bar

expression() returns a list of language objects, and we only asked for 
one.  We can look inside it:

 > e[[1]][[1]]
`<-`

The as.list function is also useful:

 > as.list(e[[1]])
[[1]]
`<-`

[[2]]
foo

[[3]]
5 * bar

and proceed recursively:

 > as.list(e[[1]][[3]])
[[1]]
`*`

[[2]]
[1] 5

[[3]]
bar


Duncan Murdoch

 >
 >> methods(class="expression")
 > no methods were found
 >> dput(expression(foo  <- 5 * bar))
 > expression(foo <- 5 * bar)
 >> str(expression(foo <- 5 * bar))
 >   expression(foo <- 5 * bar)
 >
 >
 >> it's equivalent to taking a
 >> long rambling conversation and then automatically turning it into a
 >> concise summary of what was said.  I think you must have human
 >> intervention.
 >
 > It's not really equivalent, natural language has ambiguities and 
subtleties
 > that computer languages, especially functional languages, intentionally
 > don't have.  By their nature, computer languages can be turned into parse
 > trees unambiguously and then those trees can be manipulated.
 >
 > But coincidentally I work in a Natural Language Processing group, and 
one of
 > the things we do is create exactly the kind of concise summaries you
 > describe. =)
 >



More information about the R-help mailing list