Home > Net >  why does `cat` append a space when displaying multiple strings?
why does `cat` append a space when displaying multiple strings?

Time:03-28

Let's say I have the following vector:

output <- c("foo\n", "bar\n")

If I want to display this via cat, I get a weird indentation:

> cat(output)
foo
 bar

Why is there a space before "bar"? I can get around it with paste:

> cat(paste(output, collapse = ""))
foo
bar

but I don't understand why cat is behaving in this fashion in the first place? Is this a bug, or am I missing something about how cat behaves?

CodePudding user response:

cat has a sep argument.

From ?cat:

sep: a character vector of strings to append after each element.

sep defaults to " ".

Hence:

output = c("foo\n", "bar\n")
cat(output, sep = "")
foo
bar

  •  Tags:  
  • r
  • Related