zz <- file("ex.data", "w") # open an output file connection
cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
cat("One more line\n", file = zz)
close(zz)
readLines("ex.data")
unlink("ex.data")
zz <- gzfile("ex.gz", "w") # compressed file
cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
close(zz)
readLines(zz <- gzfile("ex.gz"))
close(zz)
unlink("ex.gz")
zz # an invalid connection
zz <- bzfile("ex.bz2", "w") # bzip2-ed file
cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
close(zz)
zz # print() method: invalid connection
print(readLines(zz <- bzfile("ex.bz2")))
close(zz)
unlink("ex.bz2")
## An example of a file open for reading and writing
Tfile <- file("test1", "w+")
c(isOpen(Tfile, "r"), isOpen(Tfile, "w")) # both TRUE
cat("abc\ndef\n", file = Tfile)
readLines(Tfile)
seek(Tfile, 0, rw = "r") # reset to beginning
readLines(Tfile)
cat("ghi\n", file = Tfile)
readLines(Tfile)
Tfile # -> print() : "valid" connection
close(Tfile)
Tfile # -> print() : "invalid" connection
unlink("test1")
## We can do the same thing with an anonymous file.
Tfile <- file()
cat("abc\ndef\n", file = Tfile)
readLines(Tfile)
close(Tfile)
## Not run: ------------------------------------
# ## fifo example -- may hang even with OS support for fifos
# if(capabilities("fifo")) {
# zz <- fifo("foo-fifo", "w+")
# writeLines("abc", zz)
# print(readLines(zz))
# close(zz)
# unlink("foo-fifo")
# }
## ---------------------------------------------
## Unix examples of use of pipes
# read listing of current directory
readLines(pipe("ls -1"))
# remove trailing commas. Suppose
## Not run: ------------------------------------
# % cat data2_
# 450, 390, 467, 654, 30, 542, 334, 432, 421,
# 357, 497, 493, 550, 549, 467, 575, 578, 342,
# 446, 547, 534, 495, 979, 479
## ---------------------------------------------
# Then read this by
scan(pipe("sed -e s/,$// data2_"), sep = ",")
# convert decimal point to comma in output: see also write.table
# both R strings and (probably) the shell need \ doubled
zz <- pipe(paste("sed s/\\\\./,/ >", "outfile"), "w")
cat(format(round(stats::rnorm(48), 4)), fill = 70, file = zz)
close(zz)
file.show("outfile", delete.file = TRUE)
## Not run: ------------------------------------
# ## example for a machine running a finger daemon
#
# con <- socketConnection(port = 79, blocking = TRUE)
# writeLines(paste0(system("whoami", intern = TRUE), "\r"), con)
# gsub(" *$", "", readLines(con))
# close(con)
## ---------------------------------------------
## Not run: ------------------------------------
# ## Two R processes communicating via non-blocking sockets
# # R process 1
# con1 <- socketConnection(port = 6011, server = TRUE)
# writeLines(LETTERS, con1)
# close(con1)
#
# # R process 2
# con2 <- socketConnection(Sys.info()["nodename"], port = 6011)
# # as non-blocking, may need to loop for input
# readLines(con2)
# while(isIncomplete(con2)) {
# Sys.sleep(1)
# z <- readLines(con2)
# if(length(z)) print(z)
# }
# close(con2)
#
# ## examples of use of encodings
# # write a file in UTF-8
# cat(x, file = (con <- file("foo", "w", encoding = "UTF-8"))); close(con)
# # read a 'Windows Unicode' file
# A <- read.table(con <- file("students", encoding = "UCS-2LE")); close(con)
## ---------------------------------------------
Run the code above in your browser using DataLab