[R] Read text into a nested list

Remko Duursma remkoduursma at gmail.com
Mon Jul 16 08:51:18 CEST 2012


Dear R-list,

I am looking for a way to efficiently read a text file (see example below)
into a nested list. I can probably find an ugly way to do this, but I would
appreciate if anyone has (ideas for) efficient solutions. 

It seems to me this is a bit similar to parsing XML, could tools from the
XML package be used/changed to read custom files like these?

thanks!
remko


## Text file looks like, for example:
txt <- 
"treelist {
	Node {
		sometext
	}
	Node {
		some more text
	}
	Node {
		Component {
			some component
                        with more text lines
		}
		Component {
			another one 
		}
		Node {
			a sub node
		}
	}
}"

## The result should look like :
#  (extra points for naming the elements of the list, but not necessary!)
> treelist
[[1]]
[1] "sometext"

[[2]]
[1] "some more text"

[[3]]
[[3]][[1]]
[1] "some component
[2] "with more text lines"

[[3]][[2]]
[1] "another one"

[[3]][[3]]
[1] "a sub node"




--
View this message in context: http://r.789695.n4.nabble.com/Read-text-into-a-nested-list-tp4636613.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list