Reputation: 3017
I am trying to copy some data directly from R to the clipboard, in my Windows machine. I found in some sites that using file="clipboard" would work. And it does, but for very small datasets.
For example:
If I copy a small dataset (100 obs), it works smoothly.
df1 <- data.frame(x=runif(100))
write.table(df1, file="clipboard", sep="\t", col.names=NA)
But if I increase the observations to 10,000, it fails:
df1 <- data.frame(x=runif(10000))
write.table(df1, file="clipboard", sep="\t", col.names=NA)
The error is:
Warning message: In .External2(C_writetable, x, file, nrow(x), p, r
Any workaround to this?
Upvotes: 31
Views: 17983
Reputation: 61
I like to use only the memory I need. So I let the object.size() function figure out what I need. In addition, I usually like to source a function that I like to use frequently.
df1 <- data.frame(x=runif(10000))
write.excel <- function(x,row.names=FALSE,col.names=TRUE,...) {
write.table(x,file = paste0("clipboard-", object.size(x)),sep="\t",row.names=row.names,col.names=col.names,...)
}
write.excel(df1, FALSE, TRUE)
It would probably be prudent to check the object.size is smaller than the memory.size. And let this thing fail if necessary.
Hope this helps.
Upvotes: 6
Reputation: 2588
If you type ?connections
you will find your answer.
This is the relevant part:
"When writing to the clipboard, the output is copied to the clipboard only when the connection is closed or flushed. There is a 32Kb limit on the text to be written to the clipboard. This can be raised by using e.g. file("clipboard-128") to give 128Kb."
So, the solution is pretty straigthforward:
df1 <- data.frame(x=runif(10000))
write.table(df1, file="clipboard-16384", sep="\t", col.names=NA)
Note that the number of Kb is just an example, so you can change it as you need (I put 2^14 that should be more than enough for your data set, but you can increase it even more. Not sure which is the hard limit, though. Maybe physical memory?)
Upvotes: 52