clipr
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.)
Installation
Install from CRAN
install.packages("clipr")
Or try the development version
devtools::install_github("mdlincoln/clipr")
Usage
library("clipr")
#> Welcome to clipr. See ?write_clip for advisories on
#> writing to teh clipboard in R.
cb <- read_clip()
# Character vectors with length > 1 will be collapsed with system-appropriate
# line breaks, unless otherwise specified
cb <- write_clip(c("Text", "for", "clipboard"))
cb
#> [1] "Text\nfor\nclipboard"
cb <- write_clip(c("Text", "for", "clipboard"), breaks = ", ")
cb
#> [1] "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)
cb
#> [1] "a\tb\n1\t4\n2\t5\n3\t6"
read_clip_tbl
will try to parse clipboard contents from spreadsheets
into data frames directly.
Developing with clipr
Interactive & non-interactive use
If you use clipr in your own package, you must not call it in non-interactive sessions, as this goes against CRAN repository policy:
Packages should not write in the user’s home filespace (including clipboards), nor anywhere else on the file system apart from the R session’s temporary directory (or during installation in the location pointed to by TMPDIR: and such usage should be cleaned up). Installing into the system’s R installation (e.g., scripts to its bin directory) is not allowed.
Limited exceptions may be allowed in interactive sessions if the package obtains confirmation from the user.
For this reason, write_clip()
will error by default in non-interactive
use, which includes CRAN tests.
Linux utility availability
clipr’s functionality on X11-based systems depends on the installation
of additional software. Therefore, if you want to use clipr in your
package, you will want to take some care in how you call it, and make
sure that your package will respond gracefully if clipboard
functionality is not working as expected. You can use the function
clipr_available()
to check if the clipboard is readable and writable
by the current R session.
Linux utility availability
clipr’s functionality on X11-based systems depends on the installation
of additional software. Therefore, if you want to use clipr in your
package, you will want to take some care in how you call it, and make
sure that your package will respond gracefully if clipboard
functionality is not working as expected. You can use the function
clipr_available()
to check if the clipboard is readable and writable
by the current R session.
Testing on CRAN and CI
A few best practices will also help you responsibly test your clipr-using package on headless systems like CRAN or other testing infrastructure like Travis:
- Examples that will try to use
read_clip()
orwrite_clip()
ought to be wrapped in\dontrun{}
- Tests calling clipr should be conditionally skipped, based on the
output of
clipr_available()
. This is necessary to pass CRAN checks, as otherwisewrite_clip
will error out. - If you are using Travis.ci to check your
package build on Linux, consult the
.travis.yml
for this package, which includes code for setting theDISPLAY
andCLIPR_ALLOW
environment variables, installingxclip
andxsel
, and running a pre-build script that will set upxclip
/xsel
to run headlessly. - If you wish to display system requirements and configuration
messages to X11 users,
dr_clipr()
provides these.
Nice uses of clipr
(a non-comprehensive list)
- reprex by @jennybc takes R code on the clipboard and renders a reproducible example from it, ready to then paste on to GitHub, Stack Overflow, or the like.
- datapasta by @milesmcbain eases the copying and pasting of R objects in and out of different sources (Excel, Google Sheets).
- curlconverter by @hrbrmstr translates cURL command lines into httr calls.