1

I apologize not providing enough details in my first post... What I show now is literally all what I use to figure out the mechanics of working with data.tree.

I am trying to organize the output of numerical simulations in a tree structure for later use with the data.tree package in R. The initial (predefined) structure of my tree is (couldn't get the first two lines into the grey box):

require("data.tree")

mcdata <- Node$new("SNS")
FTS  <-                         mcdata$AddChild("First Target Station")
    BL1A <-                         FTS$AddChild("Beamline 1A")
    BL1B <-                         FTS$AddChild("Beamline 1B")
    BL1C <-                         FTS$AddChild("Beamline 1C")
    BL2  <-                         FTS$AddChild("Beamline 2")
    BL3  <-                         FTS$AddChild("Beamline 3")
    BL10 <-                         FTS$AddChild("Beamline 10")
    BL11A <-                        FTS$AddChild("Beamline 11A")
    BL11B <-                        FTS$AddChild("Beamline 11B")
    BL12  <-                        FTS$AddChild("Beamline 12")

STS  <-                         mcdata$AddChild("Second Target Station")
    BL1 <-                          STS$AddChild("Beamline 1")
    BL2 <-                          STS$AddChild("Beamline 2")
    BL3 <-                          STS$AddChild("Beamline 3")
    BL4 <-                          STS$AddChild("Beamline 4")
    BL5 <-                          STS$AddChild("Beamline 5")

Processing simulation output files results in something like

  result <- c("First Target Station", "Beamline 11A", "RAPID", ...)

'result' can get much longer but once I have a suitable way to add "RAPID" as a child I figured it should be easy to add whatever children and/or attributes come later. To decide if I need to add a child with name "RAPID" I do the following (knowing that the branches "First Target Station" and "Beamline 11A" exist).

  path <- mcdata$"First Target Station"$"Beamline 11A"$"RAPID"

If length(path) is 0 I have to add the child with name "RAPID" otherwise I don't have to do that.

This is straight forward but not really useful for practical purposes. What I think I want is a way to build the 'path' directly with the elements in result. However the naive attempt

  path <- mcdata$result[1]$result[2]$result[3]

and several variation to the theme I tried are not working...

Any suggestion how to resolve my little problem will be greatly appreciated...

Thanks,

Chris

P.S. As you can probably tell I am a new 'poster' and not much into the Object Oriented ways of doing things. I apologize in advance if this is not a good way to ask my question.

989
  • 12,579
  • 5
  • 31
  • 53
  • what is mcdata ? since you are a beginner in SO i will suggest read this thread to include reproducible code in your question.. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Dhawal Kapil Sep 13 '15 at 17:51
  • definitely, please add some more detail. this http://stackoverflow.com/questions/31339805/converting-json-format-to-csv-to-upload-data-table-in-r-to-produce-d3-bubble-cha/31352770#31352770 might help – timelyportfolio Sep 13 '15 at 18:07
  • well, data.tree has its own little way of doing things. I'll post up an answer in the next couple of hours. Thanks for the details. – timelyportfolio Sep 16 '15 at 00:58
  • assign('newPath', oldPath$AddChild(parsedFile[.]))$path – Chris Wildgruber Sep 16 '15 at 02:55
  • I was going to add that I trying to use the 'assign' statement...thanks for giving it some thought! – Chris Wildgruber Sep 16 '15 at 02:57

1 Answers1

1

I'll do this line by line. Of course, I'm guessing you are looking for a sort of automated way to handle lots of results, but I thought starting here would be best.

# stack overflow
#    http://stackoverflow.com/questions/32552231/growing-a-data-tree-in-r
require("data.tree")

mcdata <- Node$new("SNS")

FTS  <-                         mcdata$AddChild("First Target Station")
  BL1A <-                         FTS$AddChild("Beamline 1A")
  BL1B <-                         FTS$AddChild("Beamline 1B")
  BL1C <-                         FTS$AddChild("Beamline 1C")
  BL2  <-                         FTS$AddChild("Beamline 2")
  BL3  <-                         FTS$AddChild("Beamline 3")
  BL10 <-                         FTS$AddChild("Beamline 10")
  BL11A <-                        FTS$AddChild("Beamline 11A")
  BL11B <-                        FTS$AddChild("Beamline 11B")
  BL12  <-                        FTS$AddChild("Beamline 12")

STS  <-                         mcdata$AddChild("Second Target Station")
  BL1 <-                          STS$AddChild("Beamline 1")
  BL2 <-                          STS$AddChild("Beamline 2")
  BL3 <-                          STS$AddChild("Beamline 3")
  BL4 <-                          STS$AddChild("Beamline 4")
  BL5 <-                          STS$AddChild("Beamline 5")

result <- c("First Target Station", "Beamline 11A", "RAPID")
result2 <- c("First Target Station", "Beamline_notme", "RAPID")

found_node <- mcdata$Climb(result[-3])
if(!is.null(found_node)){
  found_node$AddChild(result[3])
} else {
  mcdata$Climb(result[1])$AddChild(result[2])$AddChild(result[3])
}

mcdata

found_node2 <- mcdata$Climb(result2[-3])
if(!is.null(found_node2)){
  found_node$AddChild(result2[3])
} else {
  mcdata$Climb(result2[1])$AddChild(result2[2])$AddChild(result2[3])
}

mcdata

# based on the comment
#   here is one way to work through multiple results
#   assuming each result contains the full path
lapply(
  list(result,result2),
  function(z){
    Reduce(
      function(x,y){
        node <- x$Climb(y)
        if(is.null(node)){
          node <- x$AddChild(y)
        }
        node
      },
      z,
      init = mcdata
    )
  }
)
timelyportfolio
  • 6,479
  • 30
  • 33
  • I was too slow before, he comment window timed out on me... Thanks so much for your time and effort, this will get me going! You are absolutely right of course I am planning to scan/rescan 100s of files on a regular basis and process the simulation results interactively... Chris – Chris Wildgruber Sep 18 '15 at 02:10
  • I just appended some code to work through a long list of results. Hope it helps. – timelyportfolio Sep 18 '15 at 03:06