I have been looking over the manual of the data.tree
package, however I was not able to find a way to save a data.tree
structure once you created one. Is there a way to write a data.tree structure instead of transforming it to another structure(i.e. data.frame) and then saving it?
Asked
Active
Viewed 1,421 times
2

20-roso
- 253
- 1
- 14
-
@Hack-R so how do you save it, as a csv? – 20-roso Aug 02 '16 at 13:25
-
ok guys, to be fair because I appreciate both the answers I rolled a dice to pick the accepted answer and the winner is @Vandenman. – 20-roso Aug 02 '16 at 13:36
-
No worries on that, but just FYI the R gurus (like Hadley Wickham) tell us it's computationally faster, more reliable and more efficient to save things as .RDS files rather than .RData. – Hack-R Aug 02 '16 at 13:43
-
@Hack-R thank you for the piece of information, much appreciated!!! – 20-roso Aug 02 '16 at 16:00
-
I tried to save a data.tree of 500 Mb in memory to a RData-file, but had to cancel it after it had surpassed 160 Gb. I haven't tried saving as a RDS-file though - do you know if this works? – Esben Eickhardt Sep 05 '17 at 06:43
3 Answers
1
Your question is a bit vague as I have no clue whatsoever what type of object you are referring to. However, this seems to work for me.
data("acme")
a = acme
class(a)
[1] "Node" "R6"
x = tempdir()
setwd(tempdir())
save(a, file = 'test.Rdata')
rm(a)
load('test.Rdata')
a

Vandenman
- 3,046
- 20
- 33
-
-
1Saving like this works fine. Note, however, that saved objects tend to be very large, as each Node is an environment. See here for an explanation why: http://stackoverflow.com/questions/13912867/empty-r-environment-becomes-large-file-when-saved. If that bothers too much, you can convert it to a list (e.g. `as.list`) or data.frame before saving it, and then convert it back after loading it (using `as.Node`). – Christoph Glur Aug 03 '16 at 18:22
-
@ChristophGlur makes a great point! My data.tree object of 26000+ nodes could not be saved as a RData-file due to the massive size. It just seems very slow having to convert back and forth just to save an object! – Esben Eickhardt Sep 05 '17 at 06:45
1
data(acme)
print(acme)
acme$fieldsAll
acme$count
acme$totalCount
acme$isRoot
acme$height
print(acme, "p", "cost")
outsource <- acme$IT$Outsource
class(outsource)
print(outsource)
outsource$fields
outsource$isLeaf
outsource$level
outsource$path
outsource$p
outsource$parent$name
outsource$root$name
outsource$expCost <- outsource$p * outsource$cost
print(acme, "expCost")
acme$Get("p")
acme$Do(function(x) x$expCost <- x$p * x$cost)
acme$Get("expCost", filterFun = isLeaf)
ToDataFrameTable(acme, "name", "p", "cost", "level", "pathString")
ToDataFrameTree(acme, "name", "p", "cost", "level")
ToDataFrameNetwork(acme, "p", "cost")
tree <- ToDataFrameTree(acme, "name", "p", "cost", "level")
saveRDS(tree, "tree.RDS")
it_works <- readRDS("tree.RDS")
it_works
levelName name p cost level 1 Acme Inc. Acme Inc. NA NA 1 2 ¦--Accounting Accounting NA NA 2 3 ¦ ¦--New Software New Software 0.50 1000000 3 4 ¦ °--New Accounting Standards New Accounting Standards 0.75 500000 3 5 ¦--Research Research NA NA 2 6 ¦ ¦--New Product Line New Product Line 0.25 2000000 3 7 ¦ °--New Labs New Labs 0.90 750000 3 8 °--IT IT NA NA 2 9 ¦--Outsource Outsource 0.20 400000 3 10 ¦--Go agile Go agile 0.05 250000 3 11 °--Switch to R Switch to R 1.00 50000 3 >

Hack-R
- 22,422
- 14
- 75
- 131
-
1A ok, I searched for "save" or "write" in the manual could not find anything, now it makes sense to me, thnx. – 20-roso Aug 02 '16 at 13:39
1
This is an answer to a related question: How does one save a data.tree into another object?
This is pretty important as several methods alter the original data.tree, a problem I have been running into.
# What one would expect
myDataTree_clone <- myDataTree
If you alter this "clone" you actually also alter the myDataTree object.
# What one should do instead
myDataTree_clone <- Clone(myDataTree)
Now you can work with the clone without altering the original myDataTree object.

Esben Eickhardt
- 3,183
- 2
- 35
- 56