glue
glue copied to clipboard
`glue()` and `glue_data()` documentation inaccurate on what trimming happens
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
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)
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)
Looks like this has already been clarified in https://glue.tidyverse.org/reference/trim.html
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"