Because dir()'s pattern argument should be a 'regular expression'
and "*.xml" as a regular expression matches things you probably
do not want:

  > file.create(c("one.pxml", "two.xml", "wxml.three"))
  [1] TRUE TRUE TRUE
  > dir(pattern="*.xml")
  [1] "one.pxml"   "two.xml"    "wxml.three"
  > dir(pattern="\\.xml$")
  [1] "two.xml"

You a probably thinking of 'glob' patterns, akin to what is used
in DOS and Unix shells.  You can use glob2rx to convert them to
regular expressions.
  > glob2rx("*.xml")
  [1] "^.*\\.xml$"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

From: Ashwani Rao [mailto:ashwamegh1...@gmail.com]
Sent: Monday, August 12, 2013 10:04 AM
To: William Dunlap
Cc: r-help@r-project.org
Subject: Re: [R] function closure

Thanks Bill.
Can you please tell me why did you change exp "pattern="*.xml"" to 
pattern="\\.xml$<file:///\\.xml$>".
I know that second one also makes sense.



On Mon, Aug 12, 2013 at 9:59 AM, William Dunlap 
<wdun...@tibco.com<mailto:wdun...@tibco.com>> wrote:
Change your 'fileIndex <- fileIndex + 1' to 'fileIndex <<- fileIndex + 1'
so the fileIndex in the environment of the call to generateFiles() is altered
(instead of creating and forgetting about a variable called fileIndex
in the function created by generateFiles.

  generateFiles <- function(path, pattern="\\.xml$<file:///\\.xml$>") {
      files <- dir(path, pattern=pattern, full.names=TRUE, recursive=TRUE, 
no..=TRUE)
      total <- length(files)
      fileindex <- 1
      fileGen <- function() {
          if (fileindex < total) {
              fileToReturn <- files[[fileindex]]
              print(fileindex)
              fileindex <<- fileindex + 1
              fileToReturn
          }
      }
      fileGen
  }

used as:

  > nextFile <- generateFiles("c:/temp", pattern="\\.R$<file:///\\.R$>")
  > nextFile()
  [1] 1
  [1] "c:/temp/4209/test.R"
  > nextFile()
  [1] 2
  [1] "c:/temp/a.R"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com<http://tibco.com>


> -----Original Message-----
> From: r-help-boun...@r-project.org<mailto:r-help-boun...@r-project.org> 
> [mailto:r-help-boun...@r-project.org<mailto:r-help-boun...@r-project.org>] On 
> Behalf
> Of Ashwani Rao
> Sent: Monday, August 12, 2013 9:45 AM
> To: r-help@r-project.org<mailto:r-help@r-project.org>
> Subject: [R] function closure
>
> Hi,
>
> Just for fun, I wanted to try function closures.
> Here  is simple function definition.
> The following "generateFiles" function return function "fileGen" which will
> return a a regular file in directory , every time a call it (fileGen).
>
>
> generateFiles <- function(path, pattern="*.xml")
>
> {
>
>         files <- dir(path, pattern="*.xml", full.names=TRUE, recursive=TRUE,
> no..=TRUE)
>
> total <- length(files)
>
> fileindex = 1
>
>         fileGen <- function()
>
> {
>
> if (fileindex > total)
>
> {
>
> return(NULL)
>
> }
>
> fileToReturn <- files[[fileindex]]
>
> print(fileindex)
>
> fileindex <- fileindex + 1
>
> return(fileToReturn)
>
> }
>
> return(fileGen)
>
> }
>
>
> I am incrementing the value of variable "fileindex" in the function
> "fileGen" , everytime I call this function.
>
> But the value of fileindex remains 1 , no matter how many times i call
> "fileGen()".
>
> Can you please tell me how to change the value of fileIndex so as in
> subsequent call to "fileGen" will reflect this.
>  --
> अश्वनी प्रताप
>
>       [[alternative HTML version deleted]]



--
अश्वनी प्रताप

        [[alternative HTML version deleted]]

______________________________________________
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