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

Closed
ning-y opened this issue Dec 18, 2023 · 2 comments · Fixed by #562
Closed

Add the .trim parameter to str_glue() #538

ning-y opened this issue Dec 18, 2023 · 2 comments · Fixed by #562
Labels
feature a feature request or enhancement tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day

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
@hadley
Copy link
Member

hadley commented Jul 15, 2024

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

@hadley hadley added feature a feature request or enhancement tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day labels Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants