Preserving indentation with glue

49 views Asked by At

When using the R package glue, the following code

glue("foo\n  bar")

results in

foo
bar

However, I would prefer it to produce

foo
  bar

I could do the following

. <- "  "
glue("foo\n{.}bar")

but it is ugly. Interestingly, the following works as intended:

glue('
foo
  bar')

but for some strange reason the following does not:

glue('foo
  bar')

Can you explain to me this behavior?

2

There are 2 answers

0
Nir Graham On BEST ANSWER
glue('foo\n   bar',.trim = FALSE)
2
stefan_aus_hannover On

I found this topic doing some research

The tidyverse github discussed this. They stated that this would need to be done to attain the desired output

glue("
    foo
      bar
     ")

Putting the link to the source in the comments on my answer