Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write from R to the clipboard on a mac

Tags:

clipboard

r

I am trying to use the write.table function to write to my clipboard on a mac os system. From other threads, I've tried

data <- rbind(c(1,1,2,3), c(1,1, 3, 4), c(1,4,6,7)) clip <- pipe("pbcopy", "w")                        write.table(data, file="clip")                                close(clip) 

This code does not give any error messages, but also does not copy anything to the clipboard. any suggestions?

like image 895
Lina Bird Avatar asked Jan 27 '13 11:01

Lina Bird


People also ask

How do you paste into clipboard on Mac?

How to paste on a Mac or MacBook. As above, you need to first select the text or object you want to copy or cut. Then press Command + C to copy, or Command + X to cut. Put the cursor where you want to place the text/object, and paste by pressing Command + V.

How do you copy values in R?

You can use the scan function to copy a column of numbers from Excel to R. Copy the column from Excel, run x <- scan() , type Ctrl-v to paste into R, and press enter to signal the end of input to scan .


2 Answers

I don't have any machine under OS X to test it, but I think you should use just clip instead of "clip":

data <- rbind(c(1,1,2,3), c(1,1, 3, 4), c(1,4,6,7)) clip <- pipe("pbcopy", "w")                        write.table(data, file=clip)                                close(clip) 

Here clip is an R object.

If you pass a string "clip" to the file argument R will think it is a file name, and instead of finding your data in clipboard, you will find a file in you R session working directory called "clip" with your data inside.

like image 166
juba Avatar answered Sep 29 '22 14:09

juba


This is an old question, but it still was a top hit when I was searching for how to get something on to the clipboard.

There is now a much better solution than any of the answers here: the clipr package.

clipr::write_clip() is all you need. It works on Windows, OS X, and X11.

From the help file: "write_clip() tries to be smart about writing objects in a useful manner. If passed a data.frame or matrix, it will format it using write.table for pasting into an external spreadsheet program. It will otherwise coerce the object to a character vector. auto will check the object type, otherwise table or character can be explicitly specified."

I also wrote a little helper function to get the last result onto the clipboard:

wc <- function(x = .Last.value) {   clipr::write_clip(x) } 
like image 35
jzadra Avatar answered Sep 29 '22 13:09

jzadra