如何将参数传递给 readr::read_csv_chunked 的回调函数
How to pass arguments to a callback function for readr::read_csv_chunked
我一直在研究 readr 的 read_delim_chunked
函数。基于 documentation,不清楚如何或是否可能将参数传递到回调函数中。例如,来自文档示例:
# Cars with 3 gears
f <- function(x, pos) {
dplyr::filter(x, .data[["gear"]] == 3)
}
readr::read_csv_chunked(
readr::readr_example("mtcars.csv"),
readr::DataFrameCallback$new(f),
chunk_size = 5)
# A tibble: 15 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <int> <int> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int>
1 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
2 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
3 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
这很好用。但是如果我想参数化齿轮值呢?例如,
f <- function(x, pos, gear_val) {
dplyr::filter(x, .data[["gear"]] == gear_val)
}
readr::read_csv_chunked(
readr::readr_example("mtcars.csv"),
readr::DataFrameCallback$new(f, gear_val = 3),
chunk_size = 5
)
Error in .subset2(public_bind_env, "initialize")(...) :
unused argument (gear_val = 3)
我尝试了多种尝试将参数传递给回调函数的组合,但它不起作用。有没有人知道如何做到这一点?
在这种情况下,您将使用功能/功能工厂,例如
f <- function(gear_val) {
function(x, pos) {
dplyr::filter(x, .data[["gear"]] == gear_val)
}
}
readr::read_csv_chunked(
readr::readr_example("mtcars.csv"),
readr::DataFrameCallback$new(f(gear_val = 3)),
chunk_size = 5
)
#> Parsed with column specification:
#> cols(
#> mpg = col_double(),
#> cyl = col_double(),
#> disp = col_double(),
#> hp = col_double(),
#> drat = col_double(),
#> wt = col_double(),
#> qsec = col_double(),
#> vs = col_double(),
#> am = col_double(),
#> gear = col_double(),
#> carb = col_double()
#> )
#> # A tibble: 15 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21.4 6. 258. 110. 3.08 3.22 19.4 1. 0. 3. 1.
#> 2 18.7 8. 360. 175. 3.15 3.44 17.0 0. 0. 3. 2.
#> 3 18.1 6. 225. 105. 2.76 3.46 20.2 1. 0. 3. 1.
#> 4 14.3 8. 360. 245. 3.21 3.57 15.8 0. 0. 3. 4.
#> 5 16.4 8. 276. 180. 3.07 4.07 17.4 0. 0. 3. 3.
#> 6 17.3 8. 276. 180. 3.07 3.73 17.6 0. 0. 3. 3.
#> 7 15.2 8. 276. 180. 3.07 3.78 18.0 0. 0. 3. 3.
#> 8 10.4 8. 472. 205. 2.93 5.25 18.0 0. 0. 3. 4.
#> 9 10.4 8. 460. 215. 3.00 5.42 17.8 0. 0. 3. 4.
#> 10 14.7 8. 440. 230. 3.23 5.34 17.4 0. 0. 3. 4.
#> 11 21.5 4. 120. 97. 3.70 2.46 20.0 1. 0. 3. 1.
#> 12 15.5 8. 318. 150. 2.76 3.52 16.9 0. 0. 3. 2.
#> 13 15.2 8. 304. 150. 3.15 3.44 17.3 0. 0. 3. 2.
#> 14 13.3 8. 350. 245. 3.73 3.84 15.4 0. 0. 3. 4.
#> 15 19.2 8. 400. 175. 3.08 3.84 17.0 0. 0. 3. 2.
由 reprex package (v0.2.0) 创建于 2018-03-12。
我一直在研究 readr 的 read_delim_chunked
函数。基于 documentation,不清楚如何或是否可能将参数传递到回调函数中。例如,来自文档示例:
# Cars with 3 gears
f <- function(x, pos) {
dplyr::filter(x, .data[["gear"]] == 3)
}
readr::read_csv_chunked(
readr::readr_example("mtcars.csv"),
readr::DataFrameCallback$new(f),
chunk_size = 5)
# A tibble: 15 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <int> <int> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int>
1 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
2 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
3 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
这很好用。但是如果我想参数化齿轮值呢?例如,
f <- function(x, pos, gear_val) {
dplyr::filter(x, .data[["gear"]] == gear_val)
}
readr::read_csv_chunked(
readr::readr_example("mtcars.csv"),
readr::DataFrameCallback$new(f, gear_val = 3),
chunk_size = 5
)
Error in .subset2(public_bind_env, "initialize")(...) :
unused argument (gear_val = 3)
我尝试了多种尝试将参数传递给回调函数的组合,但它不起作用。有没有人知道如何做到这一点?
在这种情况下,您将使用功能/功能工厂,例如
f <- function(gear_val) {
function(x, pos) {
dplyr::filter(x, .data[["gear"]] == gear_val)
}
}
readr::read_csv_chunked(
readr::readr_example("mtcars.csv"),
readr::DataFrameCallback$new(f(gear_val = 3)),
chunk_size = 5
)
#> Parsed with column specification:
#> cols(
#> mpg = col_double(),
#> cyl = col_double(),
#> disp = col_double(),
#> hp = col_double(),
#> drat = col_double(),
#> wt = col_double(),
#> qsec = col_double(),
#> vs = col_double(),
#> am = col_double(),
#> gear = col_double(),
#> carb = col_double()
#> )
#> # A tibble: 15 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21.4 6. 258. 110. 3.08 3.22 19.4 1. 0. 3. 1.
#> 2 18.7 8. 360. 175. 3.15 3.44 17.0 0. 0. 3. 2.
#> 3 18.1 6. 225. 105. 2.76 3.46 20.2 1. 0. 3. 1.
#> 4 14.3 8. 360. 245. 3.21 3.57 15.8 0. 0. 3. 4.
#> 5 16.4 8. 276. 180. 3.07 4.07 17.4 0. 0. 3. 3.
#> 6 17.3 8. 276. 180. 3.07 3.73 17.6 0. 0. 3. 3.
#> 7 15.2 8. 276. 180. 3.07 3.78 18.0 0. 0. 3. 3.
#> 8 10.4 8. 472. 205. 2.93 5.25 18.0 0. 0. 3. 4.
#> 9 10.4 8. 460. 215. 3.00 5.42 17.8 0. 0. 3. 4.
#> 10 14.7 8. 440. 230. 3.23 5.34 17.4 0. 0. 3. 4.
#> 11 21.5 4. 120. 97. 3.70 2.46 20.0 1. 0. 3. 1.
#> 12 15.5 8. 318. 150. 2.76 3.52 16.9 0. 0. 3. 2.
#> 13 15.2 8. 304. 150. 3.15 3.44 17.3 0. 0. 3. 2.
#> 14 13.3 8. 350. 245. 3.73 3.84 15.4 0. 0. 3. 4.
#> 15 19.2 8. 400. 175. 3.08 3.84 17.0 0. 0. 3. 2.
由 reprex package (v0.2.0) 创建于 2018-03-12。