16

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

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
mikebmassey
  • 8,354
  • 26
  • 70
  • 95

3 Answers3

20

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.

millimoose
  • 39,073
  • 9
  • 82
  • 134
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • 1
    I don't know anything about R, but I'm curious why `writeClipboard` and `readClipboard` aren't implemented on OS X if they're part of R's standard library. – bames53 Jan 27 '12 at 16:38
  • @bames53 The functions seem to be a Windows-only extension. They're not actually documented in the online manual - the help file is actually about reading from the X11 clipboard which is the only thing R seems to be able to do by default. – millimoose Jan 27 '12 at 16:45
  • I did not find this answer helpful. This answer imo is more instructive: http://stackoverflow.com/questions/14547069/how-to-write-from-r-to-the-clipboard-on-a-mac?lq=1 – Hugh Aug 05 '15 at 02:59
8

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

Community
  • 1
  • 1
Rob
  • 834
  • 1
  • 10
  • 15
  • Why not add this as a comment to Carl's answer if you're referring to it anyway? – millimoose Jan 27 '12 at 16:35
  • 2
    That was a mistake. I looked for the reply option and didn't see it. Sorry about that :) – Rob Jan 27 '12 at 16:57
  • 3
    @Inerdial for some reason it is feature of stackoverflow that you cannot leave comments until you have 50 reputation http://stackoverflow.com/privileges/comment. +1 to Rob out of empathy. – GSee Jan 28 '12 at 02:45
3

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.

Assaf
  • 525
  • 5
  • 6