根据文章https://diskframe.com/articles/ingesting-data.html,inmapfn的一个很好的用例csv_to_disk_frame(...)
是日期转换的一部分。在我的数据中,我知道运行时日期列的名称,并希望将日期输入到读取时间函数的转换。我遇到的一个问题是,除了块本身之外,似乎没有任何其他参数可以传递到 inmapfn 参数中。我不能在运行时使用硬编码变量,因为直到运行时才知道列的名称。
为了澄清这个问题,inmapfn 似乎在自己的环境中运行,以防止任何数据竞争/其他并行化问题,但我知道变量不会改变,所以我希望有办法覆盖它,因为我可以确保这是安全的。
我知道我调用的函数在任意数据帧上调用时有效。
我在下面提供了一个可重现的示例。
library(tidyverse)
library(disk.frame)
setup_disk.frame()
a <- tribble(~dates, ~val,
"09feb2021", 2,
"21feb2012", 2,
"09mar2013", 3,
"20apr2021", 4,
)
write_csv(a, "a.csv")
dates_col <- "dates"
tmp.df <- csv_to_disk.frame(
"a.csv",
outdir = file.path(tempdir(), "tmp.df"),
in_chunk_size = 1L,
inmapfn = function(chunk) {
chunk[, sdate := as.Date(do.call(`$`, list(chunk,dates_col)), "%d%b%Y")]
}
)
#> -----------------------------------------------------
#> Stage 1 of 2: splitting the file a.csv into smallers files:
#> Destination: C:\Users\joelk\AppData\Local\Temp\RtmpcFBBkr\file4a1876e87bf5
#> -----------------------------------------------------
#> Stage 1 of 2 took: 0.020s elapsed (0.000s cpu)
#> -----------------------------------------------------
#> Stage 2 of 2: Converting the smaller files into disk.frame
#> -----------------------------------------------------
#> csv_to_disk.frame: Reading multiple input files.
#> Please use `colClasses = ` to set column types to minimize the chance of a failed read
#> =================================================
#>
#> -----------------------------------------------------
#> -- Converting CSVs to disk.frame -- Stage 1 of 2:
#>
#> Converting 5 CSVs to 6 disk.frames each consisting of 6 chunks
#>
#> Error in do.call(`$`, list(chunk, dates_col)): object 'dates_col' not found