I'm using bash
to pipe data through an Rscript
like so:
cat random.csv | Rscript test.R arg >| delete.csv
My aim is to use the R
package readr
to both 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())
The above code produces the following error message at the command line:
Error Message
Error in path.expand(path) : invalid 'path' argument
Calls: write_csv -> write_delim -> normalizePath -> path.expand
Execution halted
I have also tried write_csv(df.in, file("stdout"))
and write_csv(df.in, stdout())
producing the same error message.
For reproducibility, here's a link to a random.csv
Definition of variables, by WHO for the Global Tuberculosis Report [43kb]
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())
#or
writeLines(format_csv(df.in), stdout())
Kudos to: jimhester
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With