Skip to content
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
Closed

Read from clipboard #656

danielsjf opened this issue Apr 25, 2017 · 7 comments

Comments

@danielsjf
Copy link

@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
Copy link
Member

@jennybc 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
Copy link

@msberends 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
Copy link
Member

@jimhester 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
Copy link

@msberends 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
Copy link
Member

@jimhester 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
Copy link

@msberends 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 that referenced this issue Nov 2, 2017
Fixes #656
@lock
Copy link

@lock 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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants