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

glue() and glue_data() documentation inaccurate on what trimming happens #267

Closed
ha0ye opened this issue Apr 16, 2022 · 4 comments
Closed

Comments

@ha0ye
Copy link

ha0ye commented Apr 16, 2022

The help file for glue() and glue_data() has:

Leading whitespace and blank lines from the first and last lines are automatically trimmed.

but calls the trim() function by default (when .trim == TRUE), which states:

Leading and trailing whitespace from the first and last lines is removed.

Recommended fix

Update the documentation -- adjust the text to indicate that both leading and trailing whitespace and blank lines are automatically trimmed

@ha0ye
Copy link
Author

ha0ye commented Apr 16, 2022

Update

Sorry, checked some other things. Seems that the only trimming is on leading and trailing blank lines, and not on "whitespace" as typically defined. See below reprex.

library(glue)

glue(" {letters[1:3]}")
#>  a
#>  b
#>  c
glue("{letters[1:3]} ")
#> a 
#> b 
#> c
identical(as.character(glue("{letters[1:3]} ")), c("a ", "b ", "c "))
#> [1] TRUE
glue("\n{letters[1:3]}")
#> a
#> b
#> c
glue("{letters[1:3]}\n")
#> a
#> b
#> c

Created on 2022-04-16 by the reprex package (v2.0.1)

@ha0ye
Copy link
Author

ha0ye commented Apr 16, 2022

Update 2

Some more confusing examples:

library(glue)
glue("\n  {letters[1:3]}")
#> a
#> b
#> c
glue("  {letters[1:3]}")
#>   a
#>   b
#>   c

Created on 2022-04-16 by the reprex package (v2.0.1)

@hadley
Copy link
Member

hadley commented Jan 26, 2023

Looks like this has already been clarified in https://glue.tidyverse.org/reference/trim.html

@hadley hadley closed this as completed Jan 26, 2023
@mtmorgan
Copy link

I'm also having trouble understanding what trim() does. The glue help page seems to point to PEP-257 and the Python implementation in the section at https://peps.python.org/pep-0257/#handling-docstring-indentation. But Python and glue::trim() behave differently with respect to leading / trailing whitespace, with Python doing more or less what I expect. Here are some example with Python >>> and R >

## leading / trailing whitespace
>>> trim("foo\n\n")
'foo'
> trim("foo\n\n")
[1] "foo\n"

>>> trim(" foo ")
'foo'
> trim(" foo ")
[1] " foo "

## indentation -- consistent spacing but not trailing whitespace
>>> trim("\n  foo\n    bar\n\n")
'foo\n  bar'
> trim("\n  foo\n    bar\n\n")
[1] "foo\n  bar\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants