New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read from clipboard #656

Closed
danielsjf opened this Issue Apr 25, 2017 · 7 comments

Comments

Projects
None yet
4 participants
@danielsjf

danielsjf commented Apr 25, 2017

I was wondering if reading from a clipboard will be supported in the future? There was an earlier issue which provided a work-around, but it doesn't seem to work anymore.

> x <- readr::read_csv(file("clipboard"))
Error in read_connection_(con) : can only read from a binary connection
> x <- readr::read_csv(file("clipboard", open = 'rb'))
Error in file("clipboard", open = "rb") : 
  'mode' for the clipboard must be 'r' or 'w'

read_csv can only read from a binary connection while the connection itself only allows for non-binary connections.

Using the base functions, this is quite straightforward.

> x <- read.csv("clipboard", sep = '\t')
> x
  a b c
1 1 2 3
2 4 5 6
@jennybc

This comment has been minimized.

Member

jennybc commented Apr 25, 2017

Here's another workaround that uses clipr and works today:

library(tidyverse)
library(clipr)

txt <- "v1,v2\n1.1,1.2\n2.1,2.2"
read_csv(txt)
#> # A tibble: 2 × 2
#>      v1    v2
#>   <dbl> <dbl>
#> 1   1.1   1.2
#> 2   2.1   2.2
write_clip(txt)
read_csv(paste(read_clip(), collapse = "\n"))
#> # A tibble: 2 × 2
#>      v1    v2
#>   <dbl> <dbl>
#> 1   1.1   1.2
#> 2   2.1   2.2
@msberends

This comment has been minimized.

msberends commented Oct 31, 2017

May I ask why this will not be supported?
read_delim is so much better than the base alternatives! Except for this fact...

@jimhester

This comment has been minimized.

Member

jimhester commented Oct 31, 2017

It is supported. Either use @jennybc's example of clipr (which works the same across platforms), or do it with readLines and file("clipboard").

read_csv(paste(readLines(file("clipboard"))), collapse = "\n"))
@msberends

This comment has been minimized.

msberends commented Oct 31, 2017

@jimhester That's a great suggestion. But why not comply with base R, so

read_csv("clipboard")

would internally be treated as

read_csv(paste(readLines(file("clipboard"))), collapse = "\n"))

?

It think it is far more convenient for users, and should not be too much work for the readr developers. I would even create a pull request myself if you would allow this translation 😉

@jimhester

This comment has been minimized.

Member

jimhester commented Oct 31, 2017

#732 implements read_csv("clipboard") to read from the system clipboard.

clipr::write_clip("a,b,c\n1,2,3")
readr::read_csv("clipboard")
#> # A tibble: 1 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1  1.00  2.00  3.00
@msberends

This comment has been minimized.

msberends commented Oct 31, 2017

You sir, are fantastic 👍

jimhester added a commit to jimhester/readr that referenced this issue Nov 2, 2017

jimhester added a commit to jimhester/readr that referenced this issue Nov 2, 2017

jimhester added a commit that referenced this issue Nov 2, 2017

@lock

This comment has been minimized.

lock bot commented Oct 12, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Oct 12, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.