[Rd] g"string"

Serguei Sokol
Mon, 15 Dec 2025 16:15:59 -0000

Le 15/12/2025 ? 16:02, Gabor Grothendieck a ?crit?:
> I believe that would require a change to R itself.  If you can make do
> with a workaround then
>
> 1. this works but has the drawback that you need to define your own
> string class.  Below fmt
> is an object with class "mystring".
>
>    as.mystring <- function(x) structure(x, class = "mystring")
>    "!.mystring" <- glue
>
>    fmt <- as.mystring("pi = {pi}")
>    !fmt
>    ## pi = 3.14159265358979
Nice.
Which makes me think why not to use "!" on a classical string? Before
now, it was a meaningless operation so no potential conflicts should appear.

# setting up
`!` <- function(x) UseMethod("!") # make generic
`!.default` <- base::`!` # preserve the old behavior for other classes
`!.character`=glue::glue # make act as a glue on strings

# usage
!"pi={pi}"
# pi=3.14159265358979
!FALSE
# [1] TRUE ## as before

Best,
Serguei.

>
> 2. Another workaround if you are going to subsequently pass that
> resulting string to some other
> function anyways is to preface that function with fn$.  It also
> supports expressions surrounded
> in backticks.  The function does not have to be cat.
>
>    library(gsubfn)
>    fn$cat("pi = $pi\n")
>    ## pi = 3.14159265358979
>
> On Sun, Dec 14, 2025 at 8:07?PM ivo welch <ivo.welch at ucla.edu>
wrote:
>> One of the more convenient syntax sugars of perl is the embedding of
>> variables in strings, aka, `$s="I am $a";`. R has similar
functionality
>> through library glue.  Alas, the library does not have the operator
>> flexibility to make it possible for glue to introduce a `g"I am
{a}"` .
>>   (One can define g("I am {a}") but that steals more of the
function
>> namespace.) Would be nice sugar for end users to have...
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>


--
Serguei Sokol
Ingenieur de recherche INRAE

Cellule Math?matiques
TBI, INSA/INRAE UMR 792, INSA/CNRS UMR 5504
135 Avenue de Rangueil
31077 Toulouse Cedex 04

tel: +33 5 61 55 98 49
email: sokol at insa-toulouse.fr