Skip to content
Prev 33265 / 398502 Next

Programcode and data in the same textfile

Hi Ernst.

I have found myself in a similar situation where I want to send 
code to someone with annotations that explain the different pieces
in richer ways than comments will permit. 

If you want to contain both data and code within a single document,
you will need to have some way to identify which is which so that the
software can distinguish the different elements of the document.  This
is precisely what a markup language does. And rather than inventing ad
hoc conventions, why not simply use a real markup language. XML is the most
natural one, and doing something like

<doc>
 <data>
  Sex    Response
  Male   1
  Male   2
  Female 3
  Female 4
 </data>

 <code>
  ......
 </code>
</doc>


Using the XML package, you can read the document into R
and do what you will with it.
To read the data,

 tr = xmlRoot(xmlTreeParse("myFile"))
 read.table(textConnection(xmlValue(tr[["data"]])), header=TRUE)

and to access the code text

 xmlValue(tr[["code"]])


I have a variety of different variants of this style of thing that I
occassionally add to the SXMLDocs package. But, for me at least, it is
easy to write handlers to process the different content but to leave
XML to identify them within the document.

Hope this provides some ideas for thinking about the problem
in a slightly broader light.

 D.
Ernst Hansen wrote:

  
    
Message-ID: <20030612140548.B2183@jessie.research.bell-labs.com>
In-Reply-To: <16104.29831.927591.63021@pc87.math.ku.dk>; from erhansen@math.ku.dk on Thu, Jun 12, 2003 at 02:39:35PM +0200