[R] opening multiple connections at once
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun May 18 17:29:12 CEST 2008
See ?file -- all you need to do is to call that on every file you want to
open.
There is a limit to the number of simultaneously open connections, but it
is currently more than 100. You haven't told us your OS (as requested in
the posting guide), and you may hit OS/process limits.
You have not told us what you actually want to do. It is very rare to
need to open more than a few files simultaneously, so perhaps you need to
rethink your program logic.
Here is some code that opens all the tex files in a directory and lists
all first lines and then all second lines:
testit <- function(dir)
{
files <- list.files(dir, pattern="\\.tex$")
cons <- vector("list", length(files))
for(i in seq_along(files)) cons[[i]] <- file(files[i], "r")
for(i in seq_along(cons)) print(readLines(cons[[i]], n=1))
for(i in seq_along(cons)) print(readLines(cons[[i]], n=1))
for(i in seq_along(cons)) close(cons[[i]])
}
But there are better ways to tackle that problem ....
(On unix-alikes you can set the per-process limits in the shell, and these
days the limit on descriptors is usually 256 or more. On Windows R uses
MSVCRT.DLL for which the documentation for _setmaxstdio says the default
limit is 512. The current R limit on user-generated connections is about
124.)
On Sun, 18 May 2008, Matje van de Camp wrote:
> Hi all,
>
> I'm new to R and I'm writing an R program that gets called on by a shell
> script.
> The shell script creates several directories with files, the names vary
> according to whatever I use as input to the shell script. I need to make
> connections to certain files in R (about 100 files in total). I have
> managed to either pass the directory paths or a list of all files in the
> directories into R.
> How can I make my R program open connections to all files in the
> directory or in the file list?
> I have searched the R-help archives and Google, but I can't seem to find
> an answer. Or at least no answers I can understand. Does anyone have a
> simple solution for me?
But we don't know what you tried, nor what you want to do with the
connection once opened,
> Thanks in advance!
>
> Matje
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list