arborworkflows / aRbor

aRbor, an R package with useful functions for Arbor workflows
5 stars 3 forks source link

select() inside a function doesn't work as expected #15

Closed lukejharmon closed 9 years ago

lukejharmon commented 10 years ago

I cannot figure out how to use select.treedata() inside another function.

tree<-pbtree(n=100, scale=1)
Q<-matrix(c(-1,1,1,-1),2,2)
rownames(Q)<-colnames(Q)<-1:2
x<-sim.history(tree,Q)$states
y<-setNames(as.numeric(x),names(x))

ydf <- as.data.frame(y)
ytd<-make.treedata(tree, ydf)

#either of these work
select(ytd, 2)
select(ytd, y)

# now try in a function
foo<-function(ytd, columnSelect) {
    ynew<-select(ytd, columnSelect)
    return(ynew)
}

# now these don't work, and return what was (to me) a strange error
foo(ytd, 2)
foo(ytd, y)

# I think I get what's happening - the second argument is passed through to dplyr 'select' 
# as 'columnSelect' and the variable within the function is being ignored.
# I am not sure how to fix this.
# there are hacks online but this might be a real issue:
# http://stackoverflow.com/questions/22919448/passing-function-argument-to-dplyr-select
uyedaj commented 10 years ago

One possible way to address this is by specifying: foo<-function(ytd, ...) { ynew<-select(ytd, ...) return(ynew) }

but not a permanent fix.