sebastian-c / overflow

An R package to assist people answering R questions on Stack Overflow
14 stars 12 forks source link

Linux `readClip` #20

Open trinker opened 11 years ago

trinker commented 11 years ago

I see that writeClip has an option to write to the Linux clipboard, however, readClip does not. Is this functionality going to exist for Linux in the readClip function as well?

readClip <- function(){
  OS <- Sys.info()["sysname"]

  cliptext <- switch(OS,
                   Darwin = {
                     con <- pipe("pbpaste")
                     text <- readLines(con)
                     close(con)
                     text
                   },
                   Windows = readClipboard(),
                   readLines("clipboard"))
    cliptext
}
sebastian-c commented 11 years ago

The function seems to work in Linux (on Ubuntu anyway). The switch checks for Windows and Mac, if it isn't one of those it runs readLines("clipboard") which works for me with a Ubuntu VM I'm running under Windows. I haven't tested it on any other distros, though. Are you having any problems?

trinker commented 11 years ago

Got you. My Linux box is at work and I missed the readLines("clipboard") or didn't realize you could do this (just looking at the code; didn't try it). I'll try it on Mint and if all is well I'll close this.

trinker commented 11 years ago

Here's the deal on a Linux Mint machine:

With readClip I used right click and then the function:

library(oveRflow)

i like icecream
out <- oveRflow:::readClip()
out

## out
## [1] "\001"

For write clip

x <- "I got disco fever"
oveRflow:::writeClip(x)

# From the clipboard clearly the original copy.
i like icecream

I'm pretty babyish with Linux so i don't know how to approach this. I suspect the primary clipboard clipit is overriding the clipx.

sebastian-c commented 11 years ago

Just checking I understood correctly:

You copied "i like icecream" to the clipboard and instead of that, readClip gave "\001" (something which must have been copied to that clipboard earlier). You tried writing to writeClip, but "i like icecream" remained on the clipboard.

Ok, so given those, what happens if, afer using writeClip you use middle click to paste?

trinker commented 11 years ago

Yes Sebastian, you understand correctly. I tried the middle click with the same results. I know mint comes with a pre-installed clip manager (clipit). Maybe I need to manually change this to clipx but do not know how to and googling it (change/alter clipboard Linux Mint) was not that helpful.

sebastian-c commented 11 years ago

I think Linux Mint uses the Debian packaging system, so try sudo apt-get install xclip in the Terminal. If it gives you problems, uninstall it with sudo apt-get --purge remove xclip

trinker commented 11 years ago

sebastian I had previously used: sudo apt-get install xclip but tried it again and I get the same results. as above.

sebastian-c commented 11 years ago

In all honesty then, I'm really not sure what to do at the moment. I might have to get a Mint VM up and running to test this out.

mrdwab commented 11 years ago

@trinker I think that you're right. There is something funny going on here, and I think that part of the problem is Ubuntu's clipboard management (see here). What I've noticed is that I can only get consistent results with Ubuntu's clipboard if I use a clipboard manager and select what I want to paste before using readClip or writeClip. As such, I'm not sure that there is much @sebastian-c is going to be able to do about this bug, but please try with a clipboard manager (Clipit or Diodon, for example) and see if that makes the functions work for you...

mrdwab commented 9 years ago

@trinker I've been having problems with Ubuntu's clipboard since recent RStudio updates.

I've done the following and it seems to work:

read.so <- function (sep = "", header = TRUE, stringsAsFactors = FALSE, 
          out = "mydf") 
{
  temp <- gsub("^#|^##", "", gsub("^\\s+", "", suppressWarnings(readLines(pipe("xclip -o -selection clipboard")))))
  closeAllConnections()
  temp <- read.table(text = paste(temp), header = header, stringsAsFactors = stringsAsFactors, 
                     sep = sep)
  assign(out, temp, envir = .GlobalEnv)
  message("data.frame ", dQuote(out), " created in your workspace")
  temp
}

Can you try that and see if it works for you too?

trinker commented 9 years ago

@mrdwab On Windows at the moment but this would assume the read in is a table/data.frame, but the basic idea should work.

mrdwab commented 9 years ago

@trinker It was just a quick-fix because I'm mostly copying data.frames. The xclip part would actually be added in to overflow:::readClip.

I don't know exactly with which version "clipboard" broke in RStudio. The old version of the function still works when I run R in a terminal in Ubuntu....