Simple utility functions to read from and write to the Windows, OS X, and X11 clipboards.
Simple utility functions to read and write from the system clipboards of Windows, OS X, and Unix-like systems (which require either xclip or xsel.)
Install from CRAN
Or try the development version
library("clipr")#> Welcome to clipr. See ?write_clip for advisories on writing to the clipboard in R.cb <- read_clip()
clipr is pipe-friendly, and will default to returning the same object that was passed in.
res <- write_clip(c("Text", "for", "clipboard"))res#>  "Text" "for" "clipboard"
To capture the string that clipr writes to the clipboard, specify
return_new = TRUE. Character vectors with length > 1 will be
collapsed with system-appropriate line breaks, unless otherwise
cb <- write_clip(c("Text", "for", "clipboard"), return_new = TRUE)cb#>  "Text\nfor\nclipboard"cb <- write_clip(c("Text", "for", "clipboard"), breaks = ", ", return_new = TRUE)cb#>  "Text, for, clipboard"
write_clip also tries to intelligently handle data.frames and
matrices, rendering them with
write.table so that they can be pasted
into a spreadsheet like Excel.
tbl <- data.frame(a = c(1, 2, 3), b = c(4, 5, 6))cb <- write_clip(tbl, return_new = TRUE)cb#>  "a\tb\n1\t4\n2\t5\n3\t6"
read_clip_tbl will try to parse clipboard contents from spreadsheets
into data frames directly.
See the “Developing with clipr” vignette included with this package for advisories on writing code that calls clipr functions.
(a non-comprehensive list)
Thank you to @wangyuchen for making the following suggestions:
To make clipr more pipe-friendly,
write_clip() now defaults to
return_new = FALSE, and will instead return the initial object that was passed in. To get the old behavior, pass
return_new = TRUE
In an effort to make
read_clip_tbl() more symmetrical,
write_clip() now defaults to writing out row and column names when they exist.
write_last_clip(), a wrapper function for
To comply with CRAN policy,
write_clip() will now error by default if run in
a non-interactive session. Non-interactive use must be explicitly enabled by
setting an environment variable
Documented that the default behavior when writing matrices to
col.names = FALSE
dr_clipr(), which gives informative suggestions for software and configuration requirements when accessing the clipboard on X11-based systems.
Suppress an erroneous warning on OS X / X11 systems when trying to write an empty string to the clipboard.
Fix error when
NA is passed to
write_clip(). This will now write
Fix error when passing
NULL or an empty vector (e.g.
will now write
"" to the clipboard.
Fixes a breaking bug that caused
clipr_available to erroneously return
FALSE. Thank you to @krivit for catching this.
Introduces better testing of
clipr_available to properly evaluate it on
clipr_available which checks to see if the system clipboard is
writeable/readable. This may be useful if you are developing a package that
relies on clipr and need to ensure that it will skip tests on machines (e.g.
CRAN, Travis) where the system clipboard may not be available. Thank you to
@jennybc for this suggestion.
Implements genuine testing of clipr functionality with thanks to some deft environment variable settings added by @jennybc.
Two Rstudio addins: one to copy the value returned when a highlighted expression is evaluated, and another that copies the console output.
## clipr 0.2.1
read_clip_tbl, a convenience function that takes tab-delimited
read_clip (such as that copied from a spreadsheet) and parses it
read.table. Thank you to Steve Simpson (@data-steve) for the original
write_clip(object_type = "table") has a new internal implementation
(writing to a temporary file rather than using
capture.output) which should
dramatically shorten the time it takes to write very large tables to the
clipboard. Thank you to @r2evans for this suggestion.
## clipr 0.2.0
Several changes to
write_clip - The separator to be used when writing a
character vector can now be explicitly declared using
will default to system-specific line breaks for both vectors and tables. -
write_clip will default to formatting data.frames and matrices with
write.table, allowing easy pasting of tabular objects into programs like
object_type="auto" will check the object type to decide on the
correct formatting, or the user may explicitly state
object_type="character". - clipr will default to sane system-specific
write.table(), however you may pass any custom desired options
return_new=TRUE (the default behavior) will return the
formatted character string that was passed to the system clipboard, while
write_clip(return_new=FALSE) will return the original object.
clear_clip, a wrapper function for
write_clip("") for easy
clearing of the system clipboard.
## clipr 0.1.1