[R] Programcode and data in the same textfile

Greg Louis glouis at dynamicro.on.ca
Thu Jun 12 18:28:01 CEST 2003


On 20030612 (Thu) at 1139:34 -0400, Thomas W Blackwell wrote:

> It also requires knowing how many lines of code precede
> the data lines.  But it _is_ a one-file solution, as
> requested.  Put the following 9 or 10 lines into a
> file named "r.source.file", then source it.
> 
> data.01 <- read.table(file="r.source.file", header=T,
> 	skip=4, comment.char="")[-1]
> 
>  # junk Sex      Response
>     #   Male     1
>     #   Male     2
>     #   Female   3
>     #   Female   4
> 

The nrows parameter can help by letting you put the data early in the
file:

data.01 <- read.table(file="r.source.file", header=T,
    skip=4, nrows=4, comment.char="")[-1]

    #   Sex     Response
    #   Male    1
    #   Male    2
    #   Female  3
    #   Female  4

print(data.01)
(more code)

(I got an error "line 1 did not have 4 elements" when I left the
"junk" header in place.)

> On Thu, 12 Jun 2003, Ernst Hansen wrote:
> 
> > 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().   something like
> >
> >       Sex    Respons
> >       Male   1
> >       Male   2
> >       Female 3
> >       Female 4

Obviously the above doesn't quite meet the requirement, since the data
have to be commented out -- but unless someone implements "here
documents", as another list member suggested, I don't think there's a
perfect solution.

-- 
| G r e g  L o u i s          | gpg public key: finger     |
|   http://www.bgl.nu/~glouis |   glouis at consultronics.com |
| http://wecanstopspam.org in signatures fights junk email |




More information about the R-help mailing list