Read and Write from the System Clipboard

Simple utility functions to read from and write to the Windows, OS X, and X11 clipboards.


CRANstatus. Downloads, grandtotal Travis-CIBuildStatus AppVeyor BuildStatus CoverageStatus

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:

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:

  1. Examples that will try to use read_clip() or write_clip() ought to be wrapped in \dontrun{}
  2. Tests calling clipr should be conditionally skipped, based on the output of clipr_available(). This is necessary to pass CRAN checks, as otherwise write_clip will error out.
  3. 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 the DISPLAY and CLIPR_ALLOW environment variables, installing xclip and xsel, and running a pre-build script that will set up xclip/xsel to run headlessly.
  4. 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)

  1. 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.
  2. datapasta by @milesmcbain eases the copying and pasting of R objects in and out of different sources (Excel, Google Sheets).
  3. curlconverter by @hrbrmstr translates cURL command lines into httr calls.

Matthew Lincoln

News

clipr 0.5.0

  • 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 CLIPR_ALLOW=TRUE.

  • Documented that the default behavior when writing matrices to write_clip() is col.names = FALSE

clipr 0.4.1

  • Correct a formatting error by adding and separation character to tables when they are being written with rownames.

clipr 0.4.0

  • Introduces dr_clipr(), which gives informative suggestions for software and configuration requirements when accessing the clipboard on X11-based systems.

clipr 0.3.3

  • Due to poor testing and configuration options, clipr was not delivering on its promised support for xsel :( This has now been fixed, with more complete Travis tests, and some core fixes by @milesmcbain.

clipr 0.3.2

  • 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 "NA" to the clipboard.

  • Fix error when passing NULL or an empty vector (e.g. character(0)). This will now write "" to the clipboard.

clipr 0.3.1

  • 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 Travis CI.

clipr 0.3.0

  • Introduces 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

    • Introduces read_clip_tbl, a convenience function that takes tab-delimited text from read_clip (such as that copied from a spreadsheet) and parses it with read.table. Thank you to Steve Simpson (@data-steve) for the original PR.

    • 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 breaks. breaks=NULL 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 Excel. Option object_type="auto" will check the object type to decide on the correct formatting, or the user may explicitly state object_type="table" or object_type="character". - clipr will default to sane system-specific options for write.table(), however you may pass any custom desired options via write_clip - 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.

    • Introduces clear_clip, a wrapper function for write_clip("") for easy clearing of the system clipboard.

    ## clipr 0.1.1

    • Bug fix that removes the explicit test for "Linux" in favor of a check for "xclip" or "xsel"

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("clipr")

0.5.0 by Matthew Lincoln, 2 months ago


https://github.com/mdlincoln/clipr


Report a bug at https://github.com/mdlincoln/clipr/issues


Browse source code at https://github.com/cran/clipr


Authors: Matthew Lincoln [aut, cre] , Louis Maddox [ctb] , Steve Simpson [ctb] , Jennifer Bryan [ctb]


Documentation:   PDF Manual  


GPL-3 license


Imports utils

Suggests covr, rstudioapi, testthat

System requirements: xclip (https://github.com/astrand/xclip) or xsel (http://www.vergenet.net/~conrad/software/xsel/) for accessing the X11 clipboard


Imported by AdhereR, carbonate, ctrdata, datapasta, prodigenr, rde, readr, reprex, usethis, vegalite.

Suggested by autothresholdr, detrendr, hrbrthemes, nandb, rio, vegawidget.


See at CRAN