[R] Programcode and data in the same textfile

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Jun 12 15:21:00 CEST 2003


On 12-Jun-03 Ernst Hansen wrote:
> I have the following problem.  It is not of earthshaking importance,
> but still I have spent a considerable amount of time thinking about
> it. 
> 
> PROBLEM: Is there any way I can have a single textfile that contains
> both
> 
>   a) data
> 
>   b) programcode
> 
> The program should act on the data, if the textfile is source()'ed
> into R.
> 
> 
> BOUNDARY CONDITION: I want the data written in the textfile in exactly
> the same format as I would use, if I had data in a separate textfile,
> to be read by read.table().  That is, with 'horizontal inhomogeneity'
> and 'vertical homogeneity' in the type of entries.  I want to write
> something like 
> 
>       Sex    Respons
>       Male   1
>       Male   2
>       Female 3
>       Female 4
> 
> In effect, I am asking if there is some way I can convince
> read.table(), that the data is contained in the following n lines of
> text. 

A thought which occurs to me, which (as far as I can tell) is not
already implemented (at any rate in read.table() which is where it
could have a natural home) is that, in the same spirit as

  read,table(file="stdin")

one could, if available, use

  read.table(file="<< EOT")

i.e. the "here document" style of redirection that has been a part
of Unix since approximately forever (if you take the origin of time
as 01/01/70 00:00). Then the above data could be read in from within
the source file by

  X<-read.table(header=TRUE,file="<< EOT")
  Sex    Respons
  Male   1
  Male   2
  Female 3
  Female 4
  EOT

I.e. this form of the command would take input from the following
lines until "EOT" is encountered on a line by itself. In the Unix
setup, "EOT" could be anything so long as it won't occur on a line by
itself within the data, and is not included in the content which is
read in.

Ted,


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 12-Jun-03                                       Time: 14:21:00
------------------------------ XFMail ------------------------------




More information about the R-help mailing list