Counter within a purr map/map_df function

I cant seem to get rcount to increase. filelist has 23 files in it but the counter rcount stays as 1 ?
e.g. Process 1 of 23
Process 1 of 23 etc..

purrr::map_df(.x = filelist,
.f = ~ {
rcount <- rcount + 1
print(glue::glue("Process {rcount} of {length(filelist)}"))
}
)
}

library(tidyverse)
library(glue)


filelist <- letters[1:23]

# hacky way using global scope assignment <<- 
rcount <-0

purrr::walk(.x = filelist,
              .f = ~ {
                rcount <<- rcount + 1
                print(glue::glue("Process {rcount} of {length(filelist)}"))
              }
)


#  more modular way using explicit environment assignment 
countenv <- environment()
countenv$rcount<-0

purrr::walk(.x = filelist,
            .f = ~ {
              countenv$rcount <- countenv$rcount + 1
              print(glue::glue("Process {rcount} of {length(filelist)}"))
            }
)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.