Thom Quinn
Thom Quinn

Reputation: 308

How can I write a matrix to a temporary R object rather than to the hard disk drive?

I am programming a pipeline in R that makes use of a few packages from Bioconductor that require reading from a .FASTA file (which is essentially a custom formatted .txt file).

Right now I have a matrix that I write to a .FASTA file using a function such as:

exportFASTA(matrix, file = "directory\\sample.fasta")

Then, I run the algorithm on the file location:

algorithm(file = "directory\\sample.fasta")

I want to refine my code so that I do not have to write the file to the hard drive. Instead, I write the .FASTA (i.e. .txt file) to a temporary R object that R will interpret as if such a file exists on the hard drive.

Upvotes: 0

Views: 305

Answers (1)

IRTFM
IRTFM

Reputation: 263481

There is a function textConnection that will process a character stream as if it were a file. Way back in the R 2.12 era (before the text= argument was introduced) one needed to use it to supply examples to read.table:

  >  read.table( file=textConnection("a b c\nd e f"), header=FALSE)
  V1 V2 V3
1  a  b  c
2  d  e  f
> txt <- "a b c\nd e f"
> read.table( file=textConnection(txt), header=FALSE)
  V1 V2 V3
1  a  b  c
2  d  e  f

Upvotes: 2

Related Questions