2

I have modified glue using purrr::partial and it works fine except when I try to using it together with purrr::map. There are multiple workaround but I'm confused on why this does not work?

library(purrr)
library(glue)


glue_css <- partial(glue, .open = "{{", .close = "}}")
a <- 5

glue_css("{{a}}") #OK

map(c("id1", "id2"), ~glue("{{.x}} {a}", .open = "{{", .close = "}}")) #OK
glue_css("{{a}} {a}") # OK
map(c("id1", "id2"), ~glue_css("{{.x}} {a}")) #.x not found
1
  • 4
    add .envir = parent.frame() to the partial function, otherwise it cannot access the environment where .x has been defined. Commented Nov 5, 2024 at 7:27

1 Answer 1

-1

Just following the purrr:map help recommendation.

# Instead of
x |> map(f, 1, 2, collapse = ",")
# do:
x |> map(\(x) f(x, 1, 2, collapse = ","))

for you case, I tried in my computer it works by using the new style of coding style.

library(purrr)
library(glue)


glue_css <- partial(glue, .open = "{{", .close = "}}")
a <- 5

glue_css("{{a}}") #OK

ids <- c("id1", "id2")
ids |> 
  map(\(x) glue("{{x}} {{a}}", .open = "{{", .close = "}}")) #OK

if you like my answer, pls vote it up.

Sign up to request clarification or add additional context in comments.

1 Comment

@julian may I know why down vote the answer? your answer will help me improve. thanks you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.