I cannot reproduce your problem.  You will have to
give more details.  (I assume you have already made
the suggested changes to your code - either label
the 3rd argument to your assign call 'envir=' or use
the syntax 'cpufile[[key]] <- value' instead of assign.)

To start debugging this, have your function print the
class of the object your object before you try to close it. 
You can do this by adding a cat() statement or by using
options(error=recover) so you can inspect things after
an error.

Also, get rid of the tryCatch business and the calls to
sink().  The first hides where an error might be coming
from and the latter may be hiding some printed messages
that might help you track down the problem. 

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: mohan.radhakrish...@polarisft.com
> [mailto:mohan.radhakrish...@polarisft.com]
> Sent: Wednesday, August 07, 2013 11:56 PM
> To: r-help@r-project.org
> Cc: Berend Hasselman; William Dunlap
> Subject: RE: [R] How is a file descriptor stored ?
> 
> Hi,
> 
> The file handling code sometimes throws this exception.
> 
> 
> Error in UseMethod("close") :
>   no applicable method for 'close' applied to an object of class "c
> ('integer', 'numeric')"
> 
> Is there a sample based on my code that I can test ? I want to extract the
> file descriptors from the hashmap and close them. I think that is causing
> the exception. Sometimes just closing - close(fd) - is causing this too.
> 
> Thanks,
> Mohan
> 
> 
> 
> 
> 
> 
>     RE: [R] How is a file descriptor stored ?
> 
> 
>     William Dunlap
>                    to:
>                      Berend Hasselman, mohan.radhakrish...@polarisft.com
>                                                          07-08-2013 08:01 PM
> 
> 
> 
> 
>     Cc:
>         "r-help@r-project.org"
> 
> 
> 
> 
> 
> 
> 
> 
> 
> > 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.
> 
> Or use the syntax
>     cpufile[[key]] <- file(key, "w")
> instead of
>     assign(key, file( key, "w" ), envir=cpufile)
> The former works for lists and environments and corresponds to your
> later usage of
>      listoffiles[[key]]
> to retrieve the data.
> 
> From what I've seen of your example, a list might be a better way to
> store your data, because of its copy-on-write semantics and because
> it doesn't keep a parent environment in memory.  By using
> '[[' instead of 'get' and 'assign' you minimize the number of changes
> required to switch between a list and an environment.
> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
> 
> 
> > -----Original Message-----
> > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> On Behalf
> > Of Berend Hasselman
> > Sent: Wednesday, August 07, 2013 4:10 AM
> > To: mohan.radhakrish...@polarisft.com
> > Cc: r-help@r-project.org
> > Subject: Re: [R] How is a file descriptor stored ?
> >
> >
> > On 07-08-2013, at 12:13, mohan.radhakrish...@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
> >
> > ______________________________________________
> > R-help@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.
> 
> 
> 
> 
> 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@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.

Reply via email to