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

indentation or tabs are only respected when surrounded by leading + closing blank lines #234

Closed
njtierney opened this issue Nov 5, 2021 · 4 comments

Comments

@njtierney
Copy link

Hello! 👋

Just noticed this behaviour in glue, and wanted to check if this was behaving as expected?

I am changing some print methods in greta to use glue and we wanted to have parameter values on a new line and then indented two spaces. However I noticed that in order to get this to work I needed to have a leading and trailing newline, just wanted to check if this is expected, as I was hoping that some of the below options would work, but they did not:

library(glue)

params <- "mu = 1, sigma = 0.5"

# output that I want
cat(paste0("parameters:\n", "  ", params))
#> parameters:
#>   mu = 1, sigma = 0.5

# spaces are removed
glue("parameters:\n  {params}")
#> parameters:
#> mu = 1, sigma = 0.5

#\t is not recognised
glue("parameters:\n \t{params}")
#> parameters:
#> mu = 1, sigma = 0.5

# literal space with carriage not recognised
glue("parameters:
             {params}")
#> parameters:
#> mu = 1, sigma = 0.5

# indendation only recognised when leading lines are presented?
glue("
     parameters:
       {params}
     ")
#> parameters:
#>   mu = 1, sigma = 0.5

Created on 2021-11-05 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.1 (2021-08-10)
#>  os       macOS Big Sur 10.16         
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_AU.UTF-8                 
#>  ctype    en_AU.UTF-8                 
#>  tz       Australia/Perth             
#>  date     2021-11-05                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  backports     1.2.1   2020-12-09 [1] CRAN (R 4.1.0)
#>  cli           3.1.0   2021-10-27 [1] CRAN (R 4.1.1)
#>  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.1.0)
#>  digest        0.6.28  2021-09-23 [1] CRAN (R 4.1.0)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi         0.5.0   2021-05-25 [1] CRAN (R 4.1.0)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.1.0)
#>  glue        * 1.4.2   2020-08-27 [1] CRAN (R 4.1.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.0)
#>  knitr         1.36    2021-09-29 [1] CRAN (R 4.1.0)
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.0)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.0)
#>  pillar        1.6.4   2021-10-18 [1] CRAN (R 4.1.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
#>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1   2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0  2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0  2021-09-26 [1] CRAN (R 4.1.0)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang         0.4.12  2021-10-18 [1] CRAN (R 4.1.0)
#>  rmarkdown     2.11    2021-09-14 [1] CRAN (R 4.1.0)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.1.0)
#>  stringi       1.7.5   2021-10-04 [1] CRAN (R 4.1.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
#>  styler        1.6.2   2021-09-23 [1] CRAN (R 4.1.0)
#>  tibble        3.1.5   2021-09-30 [1] CRAN (R 4.1.0)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.0)
#>  withr         2.4.2   2021-04-18 [1] CRAN (R 4.1.0)
#>  xfun          0.26    2021-09-14 [1] CRAN (R 4.1.0)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.1.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

Thanks again for providing glue! It's helping make code more readable and debuggable, just wanted to check if this was correct behaviour :)

@jimhester
Copy link
Collaborator

jimhester commented Nov 5, 2021

The idea is to make the indentation of the glue template match the indentation to the desired output, so I would suggest using something like this for this case.

glue("
parameters:
  {params}
")

The intuition should be that the values are always trimmed to the least indentation level.

That being said I think the first example should have worked as you expected, that looks to me like a bug where the indentation of the first line is not correctly considered when calculating the minimum indentation.

@jennybc
Copy link
Member

jennybc commented Nov 24, 2021

@jimhester can you say exactly which example you think should have worked?

@jimhester
Copy link
Collaborator

glue("parameters:\n  {params}")

I think should give you

parameters:
  mu = 2, sigma = 0.5

@jennybc
Copy link
Member

jennybc commented Nov 25, 2021

I've had a think on this and believe glue() is behaving as documented and as intended.

Here's how trim() says it will handle indentation and whitespace (emphasis mine):

  • Leading and trailing whitespace from the first and last lines is removed.
  • A uniform amount of indentation is stripped from the second line on, equal to the minimum indentation of all non-blank lines after the first.
  • Lines can be continued across newlines by using \\.

In glue("parameters:\n {params}"), the minimum indentation of all non-blank lines after the first line is 2. Therefore 2 spaces are stripped from all but the first line.

@njtierney I think you're sort of fighting the intended usage, which is to indent glue strings in your source similar to how you want them to appear to a user. Versus writing strings with literal \n's and such.

Personally, I'd write this as

params <- "mu = 1, sigma = 0.5"
glue::glue("
  parameters:
    {params}")
#> parameters:
#>   mu = 1, sigma = 0.5

which is functionally equivalent to what @jimhester suggested.

If you really want to insert your own literal \n newlines, then you're going to have to add an artificial one at the front so that indentation is determined based only on your "real" lines.

params <- "mu = 1, sigma = 0.5"
glue::glue("\nparameters:\n  {params}")
#> parameters:
#>   mu = 1, sigma = 0.5

@jennybc jennybc closed this as completed Nov 25, 2021
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

3 participants