0

I want to move data from a web site into R by simply copying it, and then in R use the scan command. But I need to do this operating system independent. I know that on Windows I can simply use scan("clipboard") and on MacOS scan(pipe("pbpaste")). But whatever I try on Unix I get an error "No protocol specified".

I found some discussions on this online. Among others, it was suggested that the commands

scan(file(description='clipboard'))
read.delim("X11_clipboard")
read.table(pipe("xclip -selection clipboard -o",open="r"))

might work, but neither do for me.

I am using Linux CentOS 7.

Wolfgang Rolke
  • 795
  • 4
  • 16
  • 1
    Wolfgang, looking at your profile reveals you apparently never accepted an answer to any of the questions you asked. That is ... not exactly how this site works and may want to give accepting answers you find useful a thought. – Dirk Eddelbuettel Jan 24 '21 at 20:20
  • 1
    Thanks for pointing this out, I was not aware that I should be doing this. I will take care of the old questions and make sure to do so in the future. – Wolfgang Rolke Jan 25 '21 at 13:32
  • Thumbs up. Also, in case that is unclear, you can 'upvote' questions, both the ones you asked as well as other questions, by clicking on the 'up' triangle -- that is the key of the reward system here. Ditto for downvotes if you (strongly enough) disagree with something. It is rare to see 'accepted but no upvote' but you see to have two of those which is why I took the liberty to suggest this. – Dirk Eddelbuettel Jan 25 '21 at 15:37

1 Answers1

0

If you look at other StackOverflow questions such as this one, you see xclip suggested as well as two handy aliases

alias setclip="xclip -selection c"
alias getclip="xclip -selection c -o"

which give a strong hint as to how this works. To test, I installed xclip (which is available for Ubuntu which I run), highlighted three lines from the R start up text and tried it:

edd@rob:~$ xclip -selection c -o
R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

edd@rob:~$ 

Works as advertised: xclip recovers the clipboard content, and prints it to stdout.

To use this in R, we simply need to read from that output which we can via the pipe() connection:

> res <- readLines(pipe("xclip -selection c -o"))
> str(res)
 chr [1:19] "" ...
> res[1:3]
[1] ""                                                             
[2] "R version 4.0.3 (2020-10-10) -- \"Bunny-Wunnies Freak Out\""  
[3] "Copyright (C) 2020 The R Foundation for Statistical Computing"
> 

But even that is overkill. Looking at ??clipboard suggests help(connections) which has an entire paragraph (!!) about this which includes

 ‘file’ can be used with ‘description = "clipboard"’ in mode ‘"r"’
 only.  This reads the X11 primary selection (see <URL:
 https://specifications.freedesktop.org/clipboards-spec/clipboards-latest.txt>),
 which can also be specified as ‘"X11_primary"’ and the secondary
 selection as ‘"X11_secondary"’.  On most systems the clipboard
 selection (that used by ‘Copy’ from an ‘Edit’ menu) can be
 specified as ‘"X11_clipboard"’.

and indeed:

> res2 <- readLines(file(description="clipboard"))
Warning message:
In readLines(file(description = "clipboard")) :
  incomplete final line found on 'clipboard'
> str(res2)
 chr [1:19] "" ...
> res2[1:3]
[1] ""
[2] "R version 4.0.3 (2020-10-10) -- \"Bunny-Wunnies Freak Out\""  
[3] "Copyright (C) 2020 The R Foundation for Statistical Computing"  
> 

Seemingly, you would still need xclip to write to the clipboard from R.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Dirk, thanks for your suggestions. I tried those commands, unfortunately they don't work for me. As with everything else I tried I always get the same error message "No protocol specified": – Wolfgang Rolke Jan 25 '21 at 13:29
  • > res2 <- readLines(file(description="clipboard")) No protocol specified Error in readLines(file(description = "clipboard")) : cannot open the connection In addition: Warning message: In readLines(file(description = "clipboard")) : unable to contact X11 display – Wolfgang Rolke Jan 25 '21 at 13:29
  • > res <- readLines(pipe("xclip -selection c -o")) No protocol specified Error: Can't open display: :0 – Wolfgang Rolke Jan 25 '21 at 13:29
  • One possible problem: I am not running RStudio on a local machine but inside a web browser from a server of my University, which in turn runs Unix. This whole effort is about my undergraduate students in the situation when their laptop breaks, so they have a way to do the work until their laptop gets fixed. Another possible issue: both errors talk about a problem with display. – Wolfgang Rolke Jan 25 '21 at 13:31
  • I also teach using RStudio Cloud and that is simply very different. It helps to be as specific as possible _when asking your question_. I answered it for the basic Linux case incl CentOS. RStudio Cloud is a different kettle of fish her for the clipbuffer behaviour. – Dirk Eddelbuettel Jan 25 '21 at 15:02
  • Sorry for not mentioning it in the original question, but it was not obvious to me that it mattered. Does this mean that my problem has no solution? Does anyone have another idea of handling the "get data from moodle to RStudio Cloud" problem? I should add that my moodle quizzes are randomized, so each student has different data. – Wolfgang Rolke Jan 25 '21 at 17:11
  • I don't know. I (personal view here) tend not to be too interested in things that are _known_ to be OS dependent. One of R's core strength is that it (to the extend possible given different OSs) smoothes over the difference. So I would probably tell my students to copy from one webbrowser tab (moodle) into another (RStudio) where I use the _editor_ ability to receive the content in a _file buffer_ which can then be saved, and read by R the usual way. – Dirk Eddelbuettel Jan 25 '21 at 17:31