Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the .trim parameter to str_glue() #538

Open
ning-y opened this issue Dec 18, 2023 · 1 comment
Open

Add the .trim parameter to str_glue() #538

ning-y opened this issue Dec 18, 2023 · 1 comment

Comments

@ning-y
Copy link

ning-y commented Dec 18, 2023

> as.character(str_glue("L1\t \n  \tL2"))
[1] "L1\t \nL2"
> packageVersion("stringr")
[1] ‘1.5.1
@arnaudgallou
Copy link
Contributor

arnaudgallou commented Jan 6, 2024

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().

@ning-y ning-y changed the title str_glue seems to strip whitespaces after newlines Add the .trim parameter to str_glue() Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants