Using readr's read_csv with stdin and textConnection

676 views
Skip to first unread message

Bob

unread,
Jun 12, 2015, 12:01:24 PM6/12/15
to manip...@googlegroups.com
Hi All,

I'm converting my teaching examples over to the readr package's functions. It looks like read_csv can't accept input from either stdin or textConnection. These provide handy ways to put data in the middle of a program so you don't have to worry how students have their paths set. My examples are below. I hope I've made an error or there's a bug in read_csv, but perhaps read_csv just can't work this way as read.csv can. 

Thanks,
Bob

> mystring <- "id,workshop,gender,q1,q2,q3,q4
+ 1,1,f,1,1,5,1
+ 2,2,f,2,1,4,1
+ 3,1,f,2,2,4,3
+ 4,2,NA,3,1,NA,3
+ 5,1,m,4,5,2,4
+ 6,2,m,5,4,5,5
+ 7,1,m,5,3,4,4
+ 8,2,m,4,5,5,5"
> mydata <- read_csv( textConnection("mystring") )
Error: can only read from a binary connection

Same kind of thing using stdin:

> mydata <- read_csv( stdin() )
Error: can only read from a binary connection
> id,workshop,gender,q1,q2,q3,q4
Error: unexpected ',' in "id,"
> 1,1,f,1,1,5,1
Error: unexpected ',' in "1,"
> 2,2,f,2,1,4,1
Error: unexpected ',' in "2,"
> 3,1,f,2,2,4,3
Error: unexpected ',' in "3,"
> 4,2,NA,3,1,NA,3
Error: unexpected ',' in "4,"
> 5,1,m,4,5,2,4
Error: unexpected ',' in "5,"
> 6,2,m,5,4,5,5
Error: unexpected ',' in "6,"
> 7,1,m,5,3,4,4
Error: unexpected ',' in "7,"
> 8,2,m,4,5,5,5
Error: unexpected ',' in "8,"
> # Blank line above ends input.

> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] readr_0.1.1

loaded via a namespace (and not attached):
[1] tools_3.2.0 Rcpp_0.11.6

Hadley Wickham

unread,
Jun 12, 2015, 12:05:25 PM6/12/15
to Bob, manipulatr
read_csv("id,workshop,gender,q1,q2,q3,q4
1,1,f,1,1,5,1
2,2,f,2,1,4,1
3,1,f,2,2,4,3
4,2,NA,3,1,NA,3
5,1,m,4,5,2,4
6,2,m,5,4,5,5
7,1,m,5,3,4,4
8,2,m,4,5,5,5")

;)

Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at http://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/

Bob

unread,
Jun 12, 2015, 12:44:53 PM6/12/15
to manip...@googlegroups.com, muen...@tennessee.edu
Even better! Thanks!

Jeroen Janssens

unread,
Jun 16, 2015, 8:20:13 AM6/16/15
to manip...@googlegroups.com
How about:

mydata <- read_csv(file("stdin"))

to read from standard input?

- Jeroen


Op vrijdag 12 juni 2015 18:44:53 UTC+2 schreef Bob:

Paul De Geest

unread,
Nov 28, 2015, 6:41:11 PM11/28/15
to manipulatr, muen...@tennessee.edu
But what if I need to use readLines to read in my data first?
Reply all
Reply to author
Forward
0 new messages