Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Function to copy to clipboard on Mac/OSX? [duplicate]

Tags:

clipboard

macos

r

I've seen a Windows function to copy to the clipboard in R. Is there an equivalent function for Mac OSX?

like image 860
mikebmassey Avatar asked Jan 27 '12 15:01

mikebmassey


3 Answers

From the help file for base::connections:

Mac OS X users can use pipe("pbpaste") and pipe("pbcopy", "w") to read from and write to that system's clipboard.

like image 197
Carl Witthoft Avatar answered Oct 28 '22 13:10

Carl Witthoft


Yep. Carl is exactly right. The best way is to use pbpaste/pbcopy.

Here's a good article on the details: http://langui.sh/2010/11/14/pbpaste-pbcopy-in-mac-os-x-or-terminal-clipboard-fun/

Rob


10/17/2013 - Update: As of R 3.0 the kmisc package contains the read.cb() function for multi-platform clipboard usage. Referenced from @agstudy's answer here: https://stackoverflow.com/a/14547293/168689

like image 37
Rob Avatar answered Oct 28 '22 13:10

Rob


For generic clipboard-reading in Mac, the syntax would be:

indat<-scan(pipe("pbpaste"),what=character(),...)

The ... here is just a place-holder, look up the scan help to see the very many options you have (the scan default is to expect a double variable in the what argument; character is probably safest for generic copy, or you can choose whatever is right for your case).

Similarly, for pasting into the Mac clipboard, the generic Mac syntax is

outdat<-"Hi There!"
cat(outdat, file=pipe("pbcopy","w"), ...)

Note that the nearly the same syntax should work in Windows and Linux as well; you should just replace the pipe calls with the generic connection name "clipboard".

The Kmisc package function mentioned above only works for data frames and tabular data, not for generic copy/paste.

like image 3
Assaf Avatar answered Oct 28 '22 12:10

Assaf