Try changing the assignment to use "<<-"

        fileindex <<- fileindex + 1



On Mon, Aug 12, 2013 at 12:44 PM, Ashwani Rao <[email protected]>wrote:

> 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]]
>
>
> ______________________________________________
> [email protected] 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.
>
>


-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

        [[alternative HTML version deleted]]

______________________________________________
[email protected] 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