有没有办法在同一行打印文本和变量内容?例如,
wd <- getwd()
print("Current working dir: ", wd)
我找不到任何关于允许我这样做的语法。
发布于 2013-03-24 01:08:41
您可以在print
中使用paste
print(paste0("Current working dir: ", wd))
或cat
cat("Current working dir: ", wd)
发布于 2013-03-24 01:14:02
或者使用message
message("Current working dir: ", wd)
@agstudy的答案在这里更合适
发布于 2015-02-22 04:39:13
最简单的方法是使用paste()
> paste("Today is", date())
[1] "Today is Sat Feb 21 15:25:18 2015"
paste0()
将导致以下结果:
> paste0("Today is", date())
[1] "Today isSat Feb 21 15:30:46 2015"
请注意,字符串和x之间没有默认分隔符。在字符串末尾使用空格是一种快速解决方法:
> paste0("Today is ", date())
[1] "Today is Sat Feb 21 15:32:17 2015"
然后将任一函数与print()
结合使用
> print(paste("This is", date()))
[1] "This is Sat Feb 21 15:34:23 2015"
或
> print(paste0("This is ", date()))
[1] "This is Sat Feb 21 15:34:56 2015"
正如其他用户所说,您也可以使用cat()
https://stackoverflow.com/questions/15589601
复制相似问题