How to print a blank line from within a function?

25,311

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
Share:
25,311
Rob
Author by

Rob

Updated on July 09, 2022

Comments

  • Rob
    Rob almost 2 years

    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!