stringr icon indicating copy to clipboard operation
stringr copied to clipboard

Add the .trim parameter to str_glue()

Open ning-y opened this issue 2 years ago • 1 comments

> as.character(str_glue("L1\t \n  \tL2"))
[1] "L1\t \nL2"
> packageVersion("stringr")
[1] ‘1.5.1’

ning-y avatar Dec 18 '23 07:12 ning-y

This is the normal behaviour since str_glue() is a wrapper around glue::glue() which trims some whitespaces by default. See glue's trimming rules for details, and more specifically the second bullet in your case.

So if you want to keep the indentation you should use glue::glue(.trim = FALSE):

glue::glue("L1\t \n  \tL2", .trim = FALSE) |> unclass()
#> [1] "L1\t \n  \tL2"

or add a newline followed by no spaces at the beginning of the string. The function uses spaces after the first \n not followed by an empty line to determine the amount of indentation to strip:

stringr::str_glue("\nL1\t \n  \tL2") |> unclass()
#> [1] "L1\t \n  \tL2"

That said, I think it would make sense to add the .trim parameter to str_glue().

arnaudgallou avatar Jan 06 '24 06:01 arnaudgallou

I agree that passing the .trim argument along would be useful.

hadley avatar Jul 15 '24 21:07 hadley