Print newlines with print () in R

I am trying to print a multi-line message in R. For example,

print("File not supplied.\nUsage: ./program F=filename",quote=0) 

I get a conclusion

 File not supplied.\nUsage: ./program F=filename 

instead of the desired

 File not supplied. Usage: ./program F=filename 
+91
r
Nov 01 2018-10-01
source share
3 answers

An alternative to cat() is writeLines() :

 > writeLines("File not supplied.\nUsage: ./program F=filename") File not supplied. Usage: ./program F=filename > 

The advantage is that you do not need to remember adding "\n" to the line passed to cat() to get a new line after your message. For example. compare the above with the same cat() result:

 > cat("File not supplied.\nUsage: ./program F=filename") File not supplied. Usage: ./program F=filename> 

and

 > cat("File not supplied.\nUsage: ./program F=filename","\n") File not supplied. Usage: ./program F=filename > 

The reason print() does not do what you want is because print() shows you a version of an object from the R level - in this case it is a character string. To display the string, you need to use other functions, such as cat() and writeLines() . I say "version" because accuracy can be reduced in printed numbers, and the printed object can be supplemented with additional information, for example.

+102
Nov 01 '10 at 19:07
source share

You can do it:

 cat("File not supplied.\nUsage: ./program F=filename\n") 

Note that cat has a return value of NULL .

+16
Nov 01 '10 at 17:55
source share

Using writeLines also dispenses with the "\ n" newline character using c() . How in:

 writeLines(c("File not supplied.","Usage: ./program F=filename",[additional text for third line])) 

This is useful if you plan to write a multi-line message with combined fixed and variable input, for example, [additional text for the third line] above.

+5
Jan 07 '16 at 20:35
source share



All Articles