1

I'm using readr::read_lines_chunked in the following way:

if(!require(readr)) install.packages("readr", repos = "http://cran.us.r-project.org")

mytb <- NULL
read_lines_chunked(file="/tmp/huge.xml", chunk_size=10, callback = function(xml, pos) {
   // extract values from xml into tmp
   if (is.null(mytb)) {
      users <- as_tibble(tmp)
   } else {
      users <- bind_rows(users, as_tibble(tmp)) 
   }
})

but this doesn't work as mytb always ends up being null ... how do you accumulate the results into a tibble?

SkyWalker
  • 13,729
  • 18
  • 91
  • 187
  • The variable names are hard to follow here - the two arguments in the callback function are `xml` and `pos` but then you don't use them, and `tmp` isn't declared before it's used. – Marius Dec 13 '19 at 00:32

1 Answers1

1

I found the solution. This package has a group of callback handlers that wrap the custom handler. So this is how it works:

mytb <- read_lines_chunked(file="/tmp/huge.xml", chunk_size=10, callback = DataFrameCallback$new(function(xml, pos) {
   // extract values from xml into tmp
   as_tibble(tmp)
}))

Note the DataFrameCallback$new(...) decorator and returning the tibble I want to stitch together as rbind.

SkyWalker
  • 13,729
  • 18
  • 91
  • 187