tidyverse / purrr

A functional programming toolkit for R

Home Page:https://purrr.tidyverse.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Partial evaluation of glue has unexpected behaviour

byronvickers opened this issue · comments

Using partial to set arguments to glue seems to leave the resultant partially-applied function unable to access apparently-available variables.

I'm not the first person to run into this issue (aside from it surely being a common enough use-case, there's a stack overflow question on the same topic here). However, I couldn't find any github issue raised for it so I thought it'd be worth raising the visibility in case it's unknown and/or unintended behaviour.

Raising as an issue on partial rather than glue as the unexpected behaviour seems to be due to how partial sets the environment of the resultant function rather than anything unexpected in glue's behaviour.

library(glue)
library(purrr)

g = partial(glue, .null = "NULL")
f = function(){
  x = 1
  g("The value of x is {x}")
}
f() 
#> Error in eval(parse(text = text, keep.source = FALSE), envir): object 'x' not found

Created on 2024-03-08 with reprex v2.0.2

Ah, apologies! I missed that one somehow.

Sounds like this is known and there's no intention to modify the behaviour of partial, so I'll go ahead just write a wrapper as suggested by Hadley in the linked issue.

Thanks to you and all the tidyverse folks for the hard work; it really improves the experience of working with R.