mohan.radhakrishnan at polarisft.com
2013-Aug-07 10:13 UTC
[R] How is a file descriptor stored ?
Hi, I thought that 'R' like java will allow me to store file names (keys) and file descriptors(values) in a hashmap. filelist.array <- function(n){ sink("nmon.log") cpufile <- new.env(hash=T, parent=emptyenv()) for (i in 1:n) { key <- paste("output", i, ".txt", sep = "") assign(key, file( key, "w" ), cpufile) } sink() return (cpufile) } But when I try to test it like this there is an exception [1] "Exception is Error in UseMethod(\"close\"): no applicable method for 'close' applied to an object of class \"c('integer', 'numeric')\"\n" test.simple.filelist.array <- function() { execution <- tryCatch({ sink("nmon.log") listoffiles <- filelist.array(3) for (v in ls(listoffiles)) { print(paste("Map value is [", listoffiles[[v]], "]")) fd <- listoffiles[[v]] close(fd) } sink() }, error = function(err){ print(paste("Exception is ",err)) }) } I think I am missing some fundamentals. Thanks, Mohan This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited. Visit us at http://www.polarisFT.com
On 07-08-2013, at 12:13, mohan.radhakrishnan at polarisft.com wrote:> > Hi, > I thought that 'R' like java will allow me to store file names > (keys) and file descriptors(values) in a hashmap. > > > filelist.array <- function(n){ > sink("nmon.log") > cpufile <- new.env(hash=T, parent=emptyenv()) > for (i in 1:n) { > key <- paste("output", i, ".txt", sep = "") > assign(key, file( key, "w" ), cpufile) > } > sink() > return (cpufile) > } > > But when I try to test it like this there is an exception > > [1] "Exception is Error in UseMethod(\"close\"): no applicable method for > 'close' applied to an object of class \"c('integer', 'numeric')\"\n" > > test.simple.filelist.array <- function() { > > execution <- tryCatch({ > sink("nmon.log") > listoffiles <- filelist.array(3) > for (v in ls(listoffiles)) { > print(paste("Map value is [", listoffiles[[v]], "]")) > fd <- listoffiles[[v]] > close(fd) > } > sink() > }, error = function(err){ > print(paste("Exception is ",err)) > }) > } > > I think I am missing some fundamentals. >Read the help page for assign more carefully. Use assign(key, file( key, "w" ), envir=cpufile) In your assign expression you are assigning cpufile to the third formal argument which is pos. You meant the envir argument, I presume. Berend
I don't know how many files you are planning to open, but what you also might run into is the maximum number of connections namely 125. See ?file. Jan mohan.radhakrishnan at polarisft.com schreef:> Hi, > I thought that 'R' like java will allow me to store file names > (keys) and file descriptors(values) in a hashmap. > > > filelist.array <- function(n){ > sink("nmon.log") > cpufile <- new.env(hash=T, parent=emptyenv()) > for (i in 1:n) { > key <- paste("output", i, ".txt", sep = "") > assign(key, file( key, "w" ), cpufile) > } > sink() > return (cpufile) > } > > But when I try to test it like this there is an exception > > [1] "Exception is Error in UseMethod(\"close\"): no applicable method for > 'close' applied to an object of class \"c('integer', 'numeric')\"\n" > > test.simple.filelist.array <- function() { > > execution <- tryCatch({ > sink("nmon.log") > listoffiles <- filelist.array(3) > for (v in ls(listoffiles)) { > print(paste("Map value is [", listoffiles[[v]], "]")) > fd <- listoffiles[[v]] > close(fd) > } > sink() > }, error = function(err){ > print(paste("Exception is ",err)) > }) > } > > I think I am missing some fundamentals. > > Thanks, > Mohan > > > > > > > This e-Mail may contain proprietary and confidential information and > is sent for the intended recipient(s) only. If by an addressing or > transmission error this mail has been misdirected to you, you are > requested to delete this mail immediately. You are also hereby > notified that any use, any form of reproduction, dissemination, > copying, disclosure, modification, distribution and/or publication > of this e-mail message, contents or its attachment other than by its > intended recipient/s is strictly prohibited. > > Visit us at http://www.polarisFT.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.