在 R 中,我可以阻止 print(cat("")) 返回 NULL 吗?为什么 cat(“foo”) 返回 foo>
如果我输入,
print(cat(""))
我
NULL
想使用 cat()
打印 R 脚本的进度,但我不明白为什么它最后返回 NULL
我所有连接的字符串,更重要的是,如何让它停止?
If I enter
print(cat(""))
I get
NULL
I want to use cat()
to print out the progress of an R script, but I don't understand why it is returning NULL
at the end of all of my concatenated strings, and more importantly, how to get it to stop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我也遇到过完全相同的问题。简而言之,
cat()
在 R 下有点不稳定。您没有详细说明如何尝试使用cat()
,但我建议查看paste()
。?paste
我想这可能就是您正在寻找的。
I have had the exact same problem. In a nutshell,
cat()
is a little wonky under R. You didn't go into great detail about how you are trying to usecat()
but I would suggest looking atpaste()
.?paste
I think it may be what you are looking for.
我认为没有必要使用
print(cat())
。打印消息cat()
已经足够了。这可能就是您正在寻找的:I do not see the need to use
print(cat())
. To printing a messagecat()
is already sufficient. This may be what you are looking for:为此,我经常使用
writeLines()
与strwrap()
和paste()
组合来组合循环值,如果我正在打印当前迭代的信息。strwrap()
根据需要处理长行换行,而writeLines()
意味着我不必记住添加"\n"
在我的cat()
调用结束时。这是一个使用它打印迭代指示器的示例:
给出:
For this, I often use
writeLines()
, in combination withstrwrap()
, andpaste()
to combine say the loop value if I'm printing out info on the current iteration.strwrap()
handles wrapping long lines as required, andwriteLines()
means I don't have to remember to add a"\n"
on the end of mycat()
calls.Here is an example using it to print out an iteration indicator:
Gives:
如果你想将它分配给一个变量,以便在 *apply 或函数 (x) 的循环中使用,请尝试以下操作:
名称是变量,“.y”向其中添加一个字符串,粘贴表示打印, eval 计算 print 的值,<- 将其分配给一个变量,ax 就是该变量。
If you want to assign it to a variable, for use in a LOOP of *apply or function (x), try this:
The name is the variable, the ".y" adds a string to it, paste says to print in, eval evaluates the print, <- assigns it to a variable, and ax is that variable.
我遇到了一些不同的问题,因为我想连接一些 html 文本以将长字符串包装在我的 Rmarkdown 中,并且从
cat()
中获取相同的 NULL。只需从闪亮的包中包装HTML()
就可以解决问题。I had a little bit of a different issue in that I wanted to concatenate some html text to wrap long strings in my Rmarkdown and was getting that same NULL from the
cat()
. Simply wrapping inHTML()
from the shiny package solved the problem.您所有的答案都在
?cat
的文档中。回答您的具体问题的部分是:... 和 ...
因此您无法阻止
print(cat(...))
返回NULL
,因为这就是cat
返回的内容。并且您需要显式添加换行符,例如cat("foo\n")
。All your answers are in the documentation for
?cat
. The portions that answer your specific question are:... and ...
So you can't stop
print(cat(...))
from returningNULL
because that's whatcat
returns. And you need to explicitly add newlines likecat("foo\n")
.NULL 是“cat()”的返回值。如果省略外部“print()”,您将看不到 NULL。
NULL is the return value of "cat()". If you omit the outer "print()" you won't see the NULL.