【发布时间】:2017-02-06 18:34:05
【问题描述】:
我正在使用 data.table::fread 从 shell 脚本读取输入。为了便于阅读,我想使用行继续符“\”将脚本拆分为多行。
但是,fread 似乎不喜欢多行的 shell 脚本。
例子:
library(data.table)
fread("cat test1.txt test2.txt") ## OK
现在将脚本分成两行:
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")
我缺少任何转义序列或开关吗?
如果不是,我猜这些是可能的解决方案:1)根本不拆分脚本 2)将脚本写入文件并使用 fread 调用该文件。
这些是我的设置:
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
【问题讨论】:
-
通过包含换行符,您会触发
fread相信您的输入是 csv,而不是命令行脚本。如果您想节省空格,请使用 R 本身,而不是命令行,例如,fread(paste0('cat test.txt', 'test.txt')) -
@MichaelChirico 似乎值得添加您的评论作为答案?
标签: r shell data.table