开发者

R - how can I dump contents of file to console output?

I simply want to read a file and output it in the console. print( readLines(...) ) is the best I开发者_运维技巧 can do so far, but I don't want a line-by-line identifier, I just want the file as-is.


Use writeLines instead of print. The default con for writeLines is stdout(), so writeLines(readLines(...)) sounds right.

See ?writeLines.


You could use system to call a system command.

system("cat yourfile.txt")


You have to specify a length (which can be too big), but maybe readChar would help:

cat(readChar(filename, 1e5))


I don't know exactly what you mean, but ?cat may be what you are looking for.


Here's an elegant way using dplyr

library(dplyr)
readLines("init.R") %>% paste0(collapse="\n") %>% cat

or with base R

cat(paste0(readLines("init.R"), collapse="\n"))

remember to replace init.R with the file path myfolder/myfile.R

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜