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

I use bash to transfer data via Rscript as follows: cat random.csv | Rscript test.R arg >| delete.csv cat random.csv | Rscript test.R arg >| delete.csv

My goal is to use the R readr package to read stdin and write stdout. I found the answer to stdin here .

test.r

 #!/usr/bin/Rscript suppressMessages(library(readr)) args <- commandArgs(trailingOnly = TRUE) df.in <- read_csv(file("stdin")) write_csv(df.in, path = stdout()) 

In the above code, the following command line error message appears:

Error message

 Error in path.expand(path) : invalid 'path' argument Calls: write_csv -> write_delim -> normalizePath -> path.expand Execution halted 

I also tried write_csv(df.in, file("stdout")) and write_csv(df.in, stdout()) , creating the same error message.

For reproducibility, here is a link to random.csv

Definition of variables, WHO for the Global Tuberculosis Report [43kb]

+5
source share
3 answers

readr has a format_csv function. Use this instead of write_csv :

 cat(format_csv(df.in)) 
+5
source

to read 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()) #or writeLines(format_csv(df.in), stdout()) 

Kudos: jimhester

+1
source

You can use write.table :

 write.table(x, file = "foo.csv", sep = ",", col.names = NA, qmethod = "double") 

See the help page: https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html

0
source

Source: https://habr.com/ru/post/1244216/


All Articles