tags:

views:

76

answers:

4

I'm writing an R package where the R code talks to a Java application. The Java application outputs a CSV formatted string and I want the R code to be able to directly read the string and convert it into a data.frame.

+8  A: 

Yes, look at the help for textConnection() -- the very powerful notion in R is that essentially all readers (as e.g. read.table() and its variants) access these connection object which may be a file, or a remote URL, or a pipe coming in from another app, or ... some text as in your case.

The same trick is used for so-called here documents:

> lines <- "
+ flim,flam
+ 1.2,2.2
+ 77.1,3.14
+ "
> con <- textConnection(lines)
> data <- read.csv(con)
> close(con)
> data
  flim flam
1  1.2 2.20
2 77.1 3.14
> 

Note that this is a simple way for building something but it is also costly due to the repeated parsing of all the data. There are other ways to get from Java to R, but this should get you going quickly. Efficiency comes next...

Dirk Eddelbuettel
+2  A: 

Yes. For example:

string <- "this,will,be\na,data,frame"
x <- read.csv(con <- textConnection(string), header=FALSE)
close(con)
#> x
#    V1   V2    V3
#1 this will    be
#2    a data frame
Joshua Ulrich
A: 

Suppose you have a file called tommy.csv (yes, imaginative, I know...) that has the contents of

col1 col2 \n 1 1 \n 2 2 \n 3 3

where each line is separated with an escape character "\n".

This file can be read with the help of allowEscapes argument in read.table.

> read.table("tommy.csv", header = TRUE, allowEscapes = TRUE)

  col1 col2
1 col1 col2
2    1    1
3    2    2
4    3    3

It's not perfect (modify column names...), but it's a start.

Roman Luštrik
A: 

This function wraps Dirk's answer into a convenient form. It's brilliant for answering questions on SO, where the asker has just dumped the data onscreen.

text_to_table <- function(text, ...)
{
   dfr <- read.table(tc <- textConnection(text), ...)
   close(tc)
   dfr
}

To use it, first copy the onscreen data and paste into your text editor.

foo bar baz
1 2 a
3 4 b

Now wrap it with text_to_table, quotes and any other arguments for read.table.

text_to_table("foo bar baz
1 2 a
3 4 b", header = TRUE)
Richie Cotton