R - 我需要在print()中添加显式换行符吗?

Mad*_*Seb 60 printing newline r

如何在R中使用新行字符?

myStringVariable <- "Very Nice ! I like";

myStringVariabel <- paste(myStringVariable, "\n", sep="");
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用

PS搜索这种东西时存在重大挑战,因为查询"R new line character"似乎确实混淆了google.我真的希望R有一个不同的名字.

Dav*_*son 126

R的本质意味着当你只是打印出来时,你永远不会在角色向量中有换行符.

> print("hello\nworld\n")
[1] "hello\nworld\n"
Run Code Online (Sandbox Code Playgroud)

也就是说,新行字符串中,他们只是没有得到打印为新的生产线.但是,如果要打印它们,可以使用其他功能,例如cat:

> cat("hello\nworld\n")
hello
world
Run Code Online (Sandbox Code Playgroud)

  • 另见"The R Inferno"的第8.1.22节http://www.burns-stat.com/pages/Tutor/R_inferno.pdf (12认同)

aba*_*ter 12

您也可以使用writeLines.

> writeLines("hello\nworld")
hello
world
Run Code Online (Sandbox Code Playgroud)

并且:

> writeLines(c("hello","world"))
hello
world
Run Code Online (Sandbox Code Playgroud)


小智 6

NewLine Char上的示例:

for (i in 1:5)
  {
   for (j in 1:i)
    {
     cat(j)
    }
    cat("\n")
  }
Run Code Online (Sandbox Code Playgroud)

结果:

    1
    12
    123
    1234
    12345
Run Code Online (Sandbox Code Playgroud)