gpt4 book ai didi

r - 更新 R 中三点省略号的值

转载 作者:行者123 更新时间:2023-12-04 10:32:39 31 4
gpt4 key购买 nike

我有一个功能 foo()我希望能够以两种不同的“模式”调用:一次或在 while 循环中。

我想使用一个有点通用的包装器( runtime_gateway() )并通过 ... 传递参数至 foo()在这里很有意义,所以我可以利用相同的“运行时网关逻辑”来处理具有不同参数集的任意函数。

如果运行 foo()在while循环中,我想更新一些 其参数的同时 保持其他参数的默认值或传递值。

我该怎么做?

我知道 rlang::dot_list(...)和 friend ,快速浏览了 https://github.com/r-lib/ellipsis .似乎其中任何一个只会让从省略号内容中提取值或检查省略号内容,但我不知道如何“在传输中更新它”。

正品

foo <- function(
id = "id_a",
at = Sys.time()
) {
message(stringr::str_glue("{id}: {at}"))
Sys.sleep(1)
}

runtime_gateway <- function(
fun = foo,
run_mode = c("once", "while"),
... # Args to be passed to `fun`
) {
run_mode <- match.arg(run_mode)

if (run_mode == "once") {
fun(...)
} else if (run_mode == "while") {
counter <- 0

while(counter < 3) {
# Goal: keep ellipsis value for `id` but *update* value for `at`
dots <- rlang::dots_list(...)
at <- if ("at" %in% names(dots)) {
message("`at` was passed via ellipsis:")
message(dots$at)
dots$at
} else {
Sys.time()
}

fun(at = at + 60, ...)
counter <- counter + 1
}
}
}

runtime_gateway()
#> id_a: 2020-02-21 14:09:16.779
runtime_gateway(at = lubridate::ymd_hms("2020-02-21 10:30:00"))
#> id_a: 2020-02-21 10:30:00

runtime_gateway(run_mode = "while")
#> id_a: 2020-02-21 14:10:18.897
#> id_a: 2020-02-21 14:10:19.900
#> id_a: 2020-02-21 14:10:20.902
runtime_gateway(run_mode = "while", id = "id_b")
#> id_b: 2020-02-21 14:10:21.905
#> id_b: 2020-02-21 14:10:22.906
#> id_b: 2020-02-21 14:10:23.908
runtime_gateway(run_mode = "while", at = lubridate::ymd_hms("2020-02-21 10:30:00"))
#> `at` was passed via ellipsis:
#> 2020-02-21 10:30:00
#> Error in fun(at = at + 60, ...): formal argument "at" matched by multiple actual arguments

创建于 2020-02-21 由 reprex package (v0.3.0)

最佳答案

您可以确保 dots包含 at如果不存在则添加参数,然后调度 fun使用 dots而不是 ...do.call

runtime_gateway <- function(
fun = foo,
run_mode = c("once", "while"),
... # Args to be passed to `fun`
) {
run_mode <- match.arg(run_mode)

if (run_mode == "once") {
fun(...)
} else if (run_mode == "while") {
counter <- 0

while(counter < 3) {
# Goal: keep ellipsis value for `id` but *update* value for `at`
dots <- rlang::dots_list(...)
if ("at" %in% names(dots)) {
message("`at` was passed via ellipsis:")
message(dots$at)
dots$at <- dots$at + 60
} else {
dots$at <- Sys.time() + 60
}

do.call(fun, dots)
counter <- counter + 1
}
}
}

这是输出:
runtime_gateway()
#> id_a: 2020-02-21 14:22:07

runtime_gateway(at = lubridate::ymd_hms("2020-02-21 10:30:00"))
#> id_a: 2020-02-21 10:30:00

runtime_gateway(run_mode = "while")
#> id_a: 2020-02-21 14:23:09
#> id_a: 2020-02-21 14:23:10
#> id_a: 2020-02-21 14:23:11

runtime_gateway(run_mode = "while", id = "id_b")
#> id_b: 2020-02-21 14:23:12
#> id_b: 2020-02-21 14:23:13
#> id_b: 2020-02-21 14:23:14

runtime_gateway(run_mode = "while", at = lubridate::ymd_hms("2020-02-21 10:30:00"))
#> `at` was passed via ellipsis:
#> 2020-02-21 10:30:00
#> id_a: 2020-02-21 10:31:00
#> `at` was passed via ellipsis:
#> 2020-02-21 10:30:00
#> id_a: 2020-02-21 10:31:00
#> `at` was passed via ellipsis:
#> 2020-02-21 10:30:00
#> id_a: 2020-02-21 10:31:00

创建于 2020-02-21 由 reprex package (v0.3.0)

关于r - 更新 R 中三点省略号的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338114/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com