Is it possible to write stdout using write_csv() from readr? Is it possible to write stdout using write_csv() from readr? bash bash

Is it possible to write stdout using write_csv() from readr?


There is a format_csv function for that in readr. Use this instead of write_csv:

cat(format_csv(df.in))


for reading from stdin:

df.in <- read_csv(paste(collapse = "\n", readLines(file("stdin"))))

for writing to stdout:

writeLines(format_csv(tibble(a=c(1,2))), stdout())#orwriteLines(format_csv(df.in), stdout())

Kudos to: jimhester


With write.table and write.csv from base R, you can specify the filename as "" to print to STDOUT:

file: either a character string naming a file or a connection open      for writing.  ‘""’ indicates output to the console.

Example:

$ Rscript -e 'write.csv(head(mtcars),"",quote=F)',mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carbMazda RX4,21,6,160,110,3.9,2.62,16.46,0,1,4,4Mazda RX4 Wag,21,6,160,110,3.9,2.875,17.02,0,1,4,4Datsun 710,22.8,4,108,93,3.85,2.32,18.61,1,1,4,1Hornet 4 Drive,21.4,6,258,110,3.08,3.215,19.44,1,0,3,1Hornet Sportabout,18.7,8,360,175,3.15,3.44,17.02,0,0,3,2Valiant,18.1,6,225,105,2.76,3.46,20.22,1,0,3,1