Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R passing data frame to another program using system()

Tags:

r

input

stdin

I have a data frame that I pass to another program using system(). In the current setup, I first write the contents of the dataframe to a text file, then have the system() command look for the created text file.

df1 <- runif(20)
write(df1, file="file1.txt")
system("myprogram file1.txt")

I have 2 questions:

1) Is there a way to pass a dataframe directly without writing the text file?

2) If not, is there are way to pass the data in memory as a text formatted entity without writing the file to disk?

Thanks for any suggestions.

like image 794
screechOwl Avatar asked Aug 15 '12 15:08

screechOwl


1 Answers

You can write to anything R calls connections, and that includes network sockets.

So process A can write to the network, and process B can read it without any file-on-disk involved, see help(connections) which even has a working example in the "Examples" section.

Your general topic here is serialization, and R does that for you. You can also pass data that way to other programs using tools that encode metadata about your data structure -- as for example Google's Protocol Buffers (supported in R by the RProtoBuf package).

like image 182
Dirk Eddelbuettel Avatar answered Sep 28 '22 15:09

Dirk Eddelbuettel