stringr
stringr copied to clipboard
Add the .trim parameter to str_glue()
> as.character(str_glue("L1\t \n \tL2"))
[1] "L1\t \nL2"
> packageVersion("stringr")
[1] ‘1.5.1’
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().
I agree that passing the .trim argument along would be useful.