File: output.R

package info (click to toggle)
r-cran-foreach 1.4.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 772 kB
  • ctags: 3
  • sloc: sh: 58; makefile: 1
file content (27 lines) | stat: -rw-r--r-- 866 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
library(foreach)

# define a combine function that writes the results to a file.
# note that the first argument is not a result, but the file
# object, and must be specified via the .init argument and
# returned as the value of this function.
output <- function(fobj, ...) {
  lines <- list(...)
  cat(sprintf('writing %d line(s)\n', length(lines)))
  writeLines(unlist(lines), con=fobj)
  fobj
}

# create a temporary file to write the results to
fname <- tempfile('foreach')
fobj <- file(fname, 'w')

# use ireadLines to create an iterator over the lines of the input file,
# which are converted to upper case, and processed by the output function
foreach(input=ireadLines('output.R'), .combine=output, .init=fobj,
        .multicombine=TRUE, .maxcombine=5) %do%
  toupper(input)

# display the results and clean up
close(fobj)
file.show(fname)
file.remove(fname)