glue icon indicating copy to clipboard operation
glue copied to clipboard

`glue()` and `glue_data()` documentation inaccurate on what trimming happens

Open ha0ye opened this issue 3 years ago • 2 comments

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 avatar Apr 16 '22 14:04 ha0ye

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 avatar Apr 16 '22 14:04 ha0ye

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)

ha0ye avatar Apr 16 '22 18:04 ha0ye

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

hadley avatar Jan 26 '23 20:01 hadley

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"

mtmorgan avatar Nov 26 '23 12:11 mtmorgan