Question

I want to write a function to print some output with blank lines in between. Using examples such as surv:::print.coxph, I saw that this can be done using cat("\n"). however, when I try that in a function, only the first cat("\n") gives a blank line as output, while the second does not. Here's an example:

prnt.test <- function(x){
  cat("\n")
  cat(x)
  cat("\n")
  cat(x)
}

prnt.test(x="test")

# Output:

test
test

Any ideas on how to print a blank line within the two test's?

(I'm using RStudio Version 0.98.501, R version 3.0.2 under Windows: Platform: i386-w64-mingw32/i386 (32-bit))

Many thanks!

Was it helpful?

Solution

You should cat a newline character twice. By writing cat(x); cat('\n'); cat(x), with x=="test" you if fact print("test\ntest") -- there's only one newline here.

A possible solution:

prnt.test <- function(x){
   cat(x, sep="\n\n")
}
prnt.test(c("test1", "test2", "test3"))
## test1
## 
## test2
##
## test3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top