R - new line in paste() function [duplicate]

微笑、不失礼 提交于 2019-12-03 05:41:43

You could try strsplit

msg <- "==================================================\n"
msg2 <- paste(msg, "Var:")
strsplit(msg2, "\n")[[1]]
# [1] "=================================================="
# [2] " Var:" 

This can also be used with noquote if you don't want quotes.

noquote(strsplit(msg2, "\n")[[1]])
# [1] ==================================================
# [2]  Var: 

This produces virtually the same result as if you were to use print with quote = FALSE

You can also define your own print method

print.myClass <- function(x, ...)  strsplit(x, "\n")[[1]]
class(msg2) <- "myClass"
print(msg2)
# [1] "=================================================="
# [2] " Var:"     
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!