1

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?

January
  • 16,320
  • 6
  • 52
  • 74

2 Answers2

2

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

stefan_aus_hannover
  • 1,777
  • 12
  • 13
  • https://github.com/tidyverse/glue#leading-whitespace-and-blank-lines-from-the-first-and-last-lines-are-automatically-trimmed – stefan_aus_hannover Jun 23 '23 at 19:53
  • Thank you, I mentioned that already in my edit in parallel with your answer, but I was looking for a way to achieve this on one line. Otherwise my code looks ugly ;-) – January Jun 27 '23 at 10:11
1
glue('foo\n   bar',.trim = FALSE)
Nir Graham
  • 2,567
  • 2
  • 6
  • 10