【问题标题】:How to accumulate the results of readr::read_lines_chunked?如何累积readr::read_lines_chunked的结果?
【发布时间】:2019-12-13 00:12:02
【问题描述】:

我正在通过以下方式使用readr::read_lines_chunked

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)) 
   }
})

但这不起作用,因为mytb 总是以null 结尾......你如何将结果累积到一个小标题中?

【问题讨论】:

  • 这里的变量名很难理解——回调函数中的两个参数是xmlpos,但是你不使用它们,并且之前没有声明tmp它被使用了。

标签: r readr


【解决方案1】:

我找到了解决方案。这个包有一组封装了自定义处理程序的回调处理程序。这就是它的工作原理:

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)
}))

注意 DataFrameCallback$new(...) 装饰器并返回我想要拼接在一起的小标题 rbind

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-31
    • 2020-02-13
    • 1970-01-01
    • 2012-11-08
    • 2013-07-18
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多