dariober dariober - 1 year ago 256
Bash Question

R/data.table: read multiline script with fread

I'm using

data.table::fread
to read input from a shell script. For readability I want to split the script on multiple lines using the line continuation character '\'.

However,
fread
doesn't seem to like shell scripts on multiple lines.

Examples:

library(data.table)
fread("cat test1.txt test2.txt") ## OK


Now split script on two lines:

fread("cat test1.txt \
test2.txt")
Error in fread("cat test.txt \n test.txt") :
Expected sep (' ') but new line, EOF (or other non printing character) ends field 0 when detecting types ( first): test.txt

## Same problem
fread("cat test.txt \\
test.txt")


Is there any escape sequence or switch I'm missing?

If not, these are possible solutions I guess: 1) Don't split script at all 2) write script to a file and call that file with fread.

These are my settings:

sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: CentOS release 6.7 (Final)

locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8 LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8 LC_PAPER=en_GB.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] data.table_1.10.4

loaded via a namespace (and not attached):
[1] tools_3.2.3 chron_2.3-46 tcltk_3.2.3

Answer Source

embedding within paste is an alternative:

fread(paste("cat test1.txt",
           "test2.txt"))

If you are looking for an easy way to read multiple text files, you could either use

fread("cat t*.txt")

or if the .txt files don't follow the above example pattern of file names, perhaps move them to a sub-directory (say 'data') and read them all as below:

fread("ls data | cat")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download