5

I want to copy from the Ubuntu Linux clipboard into R Studio. My workflow consists of moving back and forth between R Studio and LibreOffice Calc. I've found the following code for writing to a Linux X11 clipboard, but I don't know how to read from it.

Write to X11 Linux clipboard:

clipboard <- function(x, sep="\t", row.names=FALSE, col.names=TRUE){
     con <- pipe("xclip -selection clipboard -i", open="w")
     write.table(x, con, sep=sep, row.names=row.names, col.names=col.names)
     close(con)
}

# Examples
vec <- c(1,2,3,4)

clipboard(vec)
clipboard(vec, ",", col.names=FALSE)
clipboard(vec, " ", row.names=TRUE)

If I highlight a selection in LibreOffice Calc I'd like to paste it directly into R Studio. How do I accomplish this task? I have installed xclip in Ubuntu.

sudo apt-get install xclip
Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
stackinator
  • 5,429
  • 8
  • 43
  • 84
  • Going from LibreOffice to R, why don't you just use `read.delim("clipboard")`? That should work.... – A5C1D2H2I1M1N2O1R2T1 Aug 23 '17 at 04:21
  • `read.delim("clipboard")` does work in Windows but won't work under Linux. I'd like to know how to modify the 'write' code shown above to read data from a Linux clipboard. – stackinator Aug 25 '17 at 16:00
  • I pretty much exclusively use Linux and it works for me® if you have xclip installed. Maybe take a look at the clipboard functions [here](https://github.com/mrdwab/overflow-mrdwab/blob/master/R/clipboard.R) in the "overflow" package. They're designed for a different purpose than what you're looking at (which is partly why they are not exported). – A5C1D2H2I1M1N2O1R2T1 Aug 26 '17 at 03:28
  • Will these methods work under newer vesions of Ubuntu? I know they are switching from X11 to Wayland and I belive xclip will only work with X11? – stackinator Aug 30 '17 at 12:51

2 Answers2

4

Using xclip

You just have to reverse some of the options and functions.

The options for the xclip command need to be changed to output and the function write.table needs to be changed into read.table.

For instance:

read.table(pipe("xclip -selection clipboard -o",open="r"))

Using file()

You can use the solution provided by Anando but in the present description of that solution some details were left out.

The command read.table("clipboard") is effectively using the command .Internal(file(description, open, blocking, encoding, method, raw) which splits up in several options

  • "X11_primary" (selected text)
  • "X11_secondary" (some auxiliary copy field only used by some programs)
  • "X11_clipboard" (copied text)

The case of Ubuntu 16.04 and maybe more general Linu:x

I could not track it down easily in the source code but based on the behaviour it seems like the "clipboard" option defaults to "X11_primary" (at least it has the same behavior in Ubuntu 16.04).

If you use read.delim("X11_clipboard") in place of read.delim("clipboard") then you get the copied text instead of the selected text.

Note, that you may get an error when using X11_clipboard such as:

> read.table("X11_clipboard")
Error in file(file, "rt") : 
  X11 clipboard selection is not supported on this system

In that case you must install the Xmu header files on your system (that is the operating system, e.g. Ubuntu). I had this error in my case and resolved it by using

sudo apt-get install libxmu-dev
sudo apt-get install xorg-dev

I do not know which of the two solved it. But after this, when I recompiled the R-base from the source code, then the read.table("X11_clipboard") worked. (I could not get it working by installing from the Ubuntu repository)

  • What happens with the switch to Wayland on Ubuntu 17.10+ ? I guess we just choose the X11 session? – stackinator Nov 27 '17 at 18:50
  • I installed both mentioned packages and have this error message when I use 'read.table("clipboard")': Error in read.table("clipboard") : no lines available in input – and-bri Jul 25 '23 at 20:28
  • @and-bri That error means that there was nothing to read. For example `read.table("")` does the same. I have no computer nearby to test it, but some quick questions: did you install the packages including the header files? What happens when you use `read.delim("X11_clipboard")` or one of the other options? What R version do you use and what system do you use (Linux? Which version? Or virtual box?). Did you have something selected or copied; is there something in the clipboard? – Sextus Empiricus Jul 26 '23 at 07:19
  • Interesting. And thanks a lot Sextus for coming back to my problem. Its not important for me to that I have a solution in hours, so no hurry. 1: I dont know what header files are. I just run the commands above in the terminal in order to install the two packages. 2: all other solutions don't work as well. the output from `read.delim("X11_clipboard")` is: Error in file(file, "rt") : X11 clipboard selection is not supported on this system 3: R version 4.3.1 (2023-06-16). My OS is linux mint and running is installed as the only OS on a laptop: Linux 5.15.0-76-generic x86_64 – and-bri Jul 27 '23 at 06:31
  • 4: yes :) I put in the clipboard with ctrl+c and get at out after the try with ctrl+v Do you prefer to open a new question for this? – and-bri Jul 27 '23 at 06:31
  • The point (1) was about whether you installed the packages including the source or not. For example whether you used `sudo apt-get install libxmu-dev` (which includes the source files) or `sudo apt-get install libxmu` (which doesn't). – Sextus Empiricus Jul 27 '23 at 08:03
  • The point (4) The default `X11_primary` requires that the region *remains* selected/highlighted. When you copy the text with ctrl-v, but undo the selection (e.g. by clicking the mouse curser in some other place, which removes the selection) then from R's point of view the clipboard is empty. – Sextus Empiricus Jul 27 '23 at 08:08
  • The point (2) may indicate that for some reason you don't have the right packages installed on your OS. I had the same error message 6 years ago. I could resolve it by installing the source, but possibly more is going on (I may test it later with my computer which currently runs Ubuntu 22.04) – Sextus Empiricus Jul 27 '23 at 08:11
  • clear for point (1) and (4). then I installed it with source. And also with keeping some text selected. But I have to admit that I don't want to transport content from the current selection of the cursor, but from the memory area of the clipboard. – and-bri Jul 27 '23 at 11:16
  • when I run it with a active selection, I got this message and no data is parsed: `[1] read.delim.clipboard. <0 rows> (or 0-length row.names) Warning message: In read.table(file = file, header = header, sep = sep, quote = quote, : incomplete final line found by readTableHeader on 'clipboard'` – and-bri Jul 27 '23 at 11:17
2

Thank you A5C. The following code does work pulling clipboard data into R, although proper results are somewhat random.

read.delim("clipboard")

It tends to work only if I hit return at the end of each line in LibreOffice Calc (a lot of work). Or if I highlight the data starting with the last row, furthest column to the right, and move up and to the left. Or if highlight the data but don't copy it. Just highlight it. Don't hit CTRL+C. Highlight the data and enter your read.delim("clipboard") command and R Studio will pull it in. Strange, but true. Otherwise I get the following error.

Warning message: In read.table(file = file, header = header, sep = sep, quote = quote, : incomplete final line found by readTableHeader on 'clipboard'

stackinator
  • 5,429
  • 8
  • 43
  • 84