Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/3d82ac671c58de5117fe to your computer and use it in GitHub Desktop.
Save timelyportfolio/3d82ac671c58de5117fe to your computer and use it in GitHub Desktop.
data.tree / R6 + pipeR experiment

I don't think combining pipeR + data.tree makes a lot of sense, but that doesn't mean I won't try. If nothing else, I think it is a great example why pipeR is my favorite piper.

Links to R packages mentioned

pipeR
data.tree

Code Without Assignment

# data.tree example but using pipeR
#### readme example from data.tree vignette ####
#### https://github.com/gluc/data.tree/blob/master/vignettes/data.tree.Rmd ####
#### pipe with data.tree with R6 ####

library(pipeR)
library(data.tree)

acme <- Node$new("Acme Inc.") %>>%
  (~
     .$AddChild("Accounting")  %>>%
      (~{
        .$AddChild("New Software")
        .$AddChild("New Accounting Standards")
      })
  ) %>>%
  (~
     .$AddChild("Research") %>>%
      (~{
        .$AddChild("New Product Line")
        .$AddChild("New Labs")
      })
  ) %>>%
  (~
     .$AddChild("IT") %>>%
      (~{
        .$AddChild("Outsource")
        .$AddChild("Go agile")
        .$AddChild("Switch to R")
      })
  )

print(acme)

Code With Assignment

#### pipe with data.tree with R6 ####

library(pipeR)
library(data.tree)

acme <- Node$new("Acme Inc.") %>>%
  (~
     '<<-'( accounting , .$AddChild("Accounting") )  %>>%
      (~{
        '<<-'( software, .$AddChild("New Software") )
        '<<-'( standards, .$AddChild("New Accounting Standards") )
      })
  ) %>>%
  (~
     '<<-'( research, .$AddChild("Research") ) %>>%
      (~{
        '<<-'( newProductLine, .$AddChild("New Product Line") )
        '<<-'( newLabs, .$AddChild("New Labs") )
      })
  ) %>>%
  (~
     '<<-'( it, .$AddChild("IT") ) %>>%
      (~{
        '<<-'( outsource, .$AddChild("Outsource") )
        '<<-'( agile, .$AddChild("Go agile") )
        '<<-'( goToR, .$AddChild("Switch to R") )
      })
  )

print(acme)
# data.tree example but using pipeR
#### readme example from data.tree vignette ####
#### https://github.com/gluc/data.tree/blob/master/vignettes/data.tree.Rmd ####
#### pipe with data.tree with R6 ####
library(pipeR)
library(data.tree)
acme <- Node$new("Acme Inc.") %>>%
(~
.$AddChild("Accounting") %>>%
(~{
.$AddChild("New Software")
.$AddChild("New Accounting Standards")
})
) %>>%
(~
.$AddChild("Research") %>>%
(~{
.$AddChild("New Product Line")
.$AddChild("New Labs")
})
) %>>%
(~
.$AddChild("IT") %>>%
(~{
.$AddChild("Outsource")
.$AddChild("Go agile")
.$AddChild("Switch to R")
})
)
print(acme)
### now if we want to do the assignment piece also
#### pipe with data.tree with R6 ####
library(pipeR)
library(data.tree)
acme <- Node$new("Acme Inc.") %>>%
(~
'<<-'( accounting , .$AddChild("Accounting") ) %>>%
(~{
'<<-'( software, .$AddChild("New Software") )
'<<-'( standards, .$AddChild("New Accounting Standards") )
})
) %>>%
(~
'<<-'( research, .$AddChild("Research") ) %>>%
(~{
'<<-'( newProductLine, .$AddChild("New Product Line") )
'<<-'( newLabs, .$AddChild("New Labs") )
})
) %>>%
(~
'<<-'( it, .$AddChild("IT") ) %>>%
(~{
'<<-'( outsource, .$AddChild("Outsource") )
'<<-'( agile, .$AddChild("Go agile") )
'<<-'( goToR, .$AddChild("Switch to R") )
})
)
print(acme)
ls.str()
@gluc
Copy link

gluc commented Apr 17, 2015

I didn't know R was capable of such crazy stuff ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment