Ernst Hansen ([EMAIL PROTECTED]) had asked to combine code 
and data in a single text file that can be sourced such that
the data is included in the sourced file a way that can be 
read by read.table.

He has already summarized the responses and the solution below
builds on that summary.  It has the advantages of allowing 
multiple embedded data files and comments within the data.  
Its also reasonably simple requiring only (1) a one line 
function acting as an alternative to stdin() and (2) a call 
to this new function within the read.table, scan or other 
read statement.

It would be even cleaner just to use stdin() but unfortunately
stdin() does not work in sourced files (bug?). Thus, this 
solution can be regarded as a workaround to that.

To run the example below, place it in the a text file called 
myFile.r in the top level directory and source it from the R
command line:
source("/myFile.r")

# start of example
myFile <- "/myFile.r"

my.stdin <- function( filename, tag ) 
  textConnection( sub(tag, "", grep(tag,readLines(filename),value=T)) )

x <- read.table( my.stdin(myFile,"^#x"), header=T )
#x Sex Response  # this example has a header
#x Male 1  
#x Male 2 
#x Female 3 
#x Female 4 

y <- read.table( my.stdin(myFile,"^#y") )
#y 3.4 4  # this example has no header
#y 3 3 
#y 6 6 

z <- scan( my.stdin(myFile,"^#z") )
#z 3 5 4 6 7
#z 8

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Reply via email to