我需要用 print()添加显式的新行字符吗?

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

myStringVariable <- "Very Nice ! I like";


myStringVariabel <- paste(myStringVariable, "\n", sep="");

以上代码 没用的

另外,在谷歌搜索这类东西的时候会遇到很大的挑战,因为“ R 新行字符”的查询确实会让谷歌感到困惑。我真希望 R 有个别的名字。

266350 次浏览

The nature of R means that you're never going to have a newline in a character vector when you simply print it out.

> print("hello\nworld\n")
[1] "hello\nworld\n"

That is, the newlines are in the string, they just don't get printed as new lines. However, you can use other functions if you want to print them, such as cat:

> cat("hello\nworld\n")
hello
world

Example on NewLine Char:

for (i in 1:5)
{
for (j in 1:i)
{
cat(j)
}
cat("\n")
}

Result:

    1
12
123
1234
12345

You can also use writeLines.

> writeLines("hello\nworld")
hello
world

And also:

> writeLines(c("hello","world"))
hello
world