stringr::str_glue() 不适用于 purrr::map()

问题描述 投票:0回答:1

有谁知道为什么以下不起作用?似乎

stringr::str_glue()
无法找到
file_name
参数,因为它位于
purrr::map()
内部。我该如何解决这个问题?

library(tidyverse)

fn_render_ce_template <- function(
    template,
    file_name, 
    early_billing_cycle = NA,
    late_billing_cycle = NA,
    early_price = NA,
    late_price = NA,
    price_dollars = NA,
    price_cents = NA,
    year = NA
){
  template %>% 
    map_chr(str_glue) %>% 
    str_squish()
}

# this doesn't work
fn_render_ce_template(
  template = c('{file_name}_1', '{file_name}_2'), 
  file_name = 'test'
)
#> Error:
#> i In index: 1.
#> Caused by error:
#> ! object 'file_name' not found
#> Backtrace:
#>      x
#>   1. +-global fn_render_ce_template(...)
#>   2. | \-template %>% map_chr(str_glue) %>% str_squish()
#>   3. +-stringr::str_squish(.)
#>   4. | +-stringi::stri_trim_both(str_replace_all(string, "\\s+", " "))
#>   5. | \-stringr::str_replace_all(string, "\\s+", " ")
#>   6. |   \-stringr:::check_lengths(string, pattern, replacement)
#>   7. |     \-vctrs::vec_size_common(...)
#>   8. +-purrr::map_chr(., str_glue)
#>   9. | \-purrr:::map_("character", .x, .f, ..., .progress = .progress)
#>  10. |   +-purrr:::with_indexed_errors(...)
#>  11. |   | \-base::withCallingHandlers(...)
#>  12. |   +-purrr:::call_with_cleanup(...)
#>  13. |   \-stringr (local) .f(.x[[i]], ...)
#>  14. |     \-glue::glue(..., .sep = .sep, .envir = .envir)
#>  15. |       \-glue::glue_data(...)
#>  16. +-glue (local) `<fn>`("file_name")
#>  17. | +-.transformer(expr, env) %||% .null
#>  18. | \-glue (local) .transformer(expr, env)
#>  19. |   \-base::eval(parse(text = text, keep.source = FALSE), envir)
#>  20. |     \-base::eval(parse(text = text, keep.source = FALSE), envir)
#>  21. \-base::.handleSimpleError(...)
#>  22.   \-purrr (local) h(simpleError(msg, call))
#>  23.     \-cli::cli_abort(...)
#>  24.       \-rlang::abort(...)

# but this does?
file_name <- 'test'

fn_render_ce_template(
  template = c('{file_name}_1', '{file_name}_2'), 
  file_name = 'test'
)
#> [1] "test_1" "test_2"

创建于 2023-12-23,使用 reprex v2.0.2

r tidyverse purrr r-glue
1个回答
0
投票

定义匿名函数

简短的回答是,这与评估变量的环境有关。您可以通过在

map_chr()
调用中定义匿名函数来解决它:

render_template <- function(template, file_name) {
    template |>
        map_chr(\(x) str_glue(x))
}

这将返回所需的输出:

render_template(
    template = c("{file_name}_1", "{file_name}_2"),
    file_name = "test"
)
# [1] "test_1" "test_2"

说明

要了解为什么会发生这种情况,请查看 str_glue() 的函数

definition
:

str_glue <- function(..., .sep = "", .envir = parent.frame()) {
  glue::glue(..., .sep = .sep, .envir = .envir)
}

默认情况下,它会在父框架中查找。当您使用

purrr::map_chr()
时,它最终会调用
purrr::map_()
,并且父框架不是正确的框架。

您可以定义一个最小函数来了解正在发生的情况:

render_template <- function(template, file_name, stack_frame_num) {
    template |>
        map_chr(\(x) str_glue(x, .envir = rlang::caller_env(stack_frame_num)))
}

如果我们使用堆栈帧号

1
调用此函数,我们将获得全局变量。

file_name <- "global"
render_template(
    template = "{file_name}_1",
    file_name = "local",
    stack_frame_num = 1
)
# [1] "global_1"

这同样适用于栈帧号为 2 的情况。但是,如果我们使用栈帧号为 3 来调用它:

render_template(
    template = "{file_name}_1",
    file_name = "local",
    stack_frame_num = 3
)
# [1] "local_1"

幸运的是,当匿名函数工作时,我们不需要手动传递堆栈帧编号,但希望这能让您了解为什么会发生这种行为。

© www.soinside.com 2019 - 2024. All rights reserved.