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?
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.
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 .
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.
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) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With