6

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.

1
  • How long is a typical data frame? One option would be to pass the data as parameters to the command, but I think what you've already done is probably cleaner. Commented Aug 15, 2012 at 15:49

2 Answers 2

8

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).

Sign up to request clarification or add additional context in comments.

Comments

0

I spent quite a while and couldn't understand the accepted answer. But I figured out a workaround.

df1 <- runif(20)
system("myprogram /dev/stdin", input = write.table(df1))

However, according to documentation, the input argument will actually be redirected to a temp file, which I suppose will involve some i/o.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.