【问题标题】:in R, can I stop print(cat("")) from returning NULL? and why does cat("foo") return foo>在 R 中,我可以停止 print(cat("")) 返回 NULL 吗?为什么 cat("foo") 返回 foo>
【发布时间】:2011-05-02 15:06:34
【问题描述】:

如果我进入

print(cat(""))

我明白了

NULL

我想使用cat() 打印出R 脚本的进度,但我不明白为什么它在我所有连接字符串的末尾返回NULL,更重要的是,如何获取它停止了吗?

【问题讨论】:

  • 您能否补充一些说明?你是如何运行这个 R 脚本的(例如,通过Rscriptsource() 等)?您想在哪里打印进度(例如控制台、文件)?
  • cat("foo") 返回返回 foo> 因为您忘记在字符串末尾附加换行符。 R 执行您的要求并打印出字符串"foo",然后打印提示符>。例如,试试cat("foo\n"),或者更好的是writeLines("foo")。我在回答中举了一些后者的例子。

标签: r concatenation paste cat


【解决方案1】:

您的所有答案都在?cat 的文档中。回答您的具体问题的部分是:

参数:

fill: a logical or (positive) numeric controlling how the output is
      broken into successive lines.  If ‘FALSE’ (default), only
      newlines created explicitly by ‘"\n"’ are printed.
      Otherwise, the output is broken into lines with print width
      equal to the option ‘width’ if ‘fill’ is ‘TRUE’, or the value
      of ‘fill’ if this is numeric.  Non-positive ‘fill’ values
      are ignored, with a warning.

...和...

价值:

 None (invisible ‘NULL’).

因此,您不能阻止 print(cat(...)) 返回 NULL,因为这就是 cat 返回的内容。你需要明确地添加像cat("foo\n")这样的换行符。

【讨论】:

  • 感谢您的回答 - 我在发布问题之前确实阅读了文档,但我仍然不清楚是否有办法阻止它打印“NULL”或为什么它打印 null 或为什么值是“None”,即使它清楚地返回提供的字符串......我想我的一些误解更多地与理论有关而不是实际。
  • 好吧,文档可以很简洁,printcat 可能看起来他们做类似的事情。但是cat 返回提供的字符串,它打印它。微妙但重要的区别。浏览“另请参阅”部分中的函数的文档页面也是一种好习惯。
  • 在回复@gunkster 时提到,我认为我不能使用 paste() 换行 ...
  • @David - 如果插入换行符,您可以获得换行符:例如cat(paste("foo","fi","\n","\n","bar\n"))writeLines(paste("foo","fi","\n","\n","bar\n"))。或者查看我的答案,了解使用 strwrap()writeLines() 以干净整洁的方式处理包装的方法。
  • @ucfagls:你不需要两个cat(paste(...))cat 自动用空格分隔参数...这样可以节省一些输入。 ;-)
【解决方案2】:

我也遇到了同样的问题。简而言之,cat() 在 R 下有点不稳定。您没有详细说明如何尝试使用 cat(),但我建议您查看 paste()

?paste

我想这可能就是你要找的东西。

【讨论】:

  • 我从 paste() 函数开始,但我无法让它添加换行符,当我向 r-help 列表询问换行符时向我推荐了 cat() :@ 987654321@
  • @David - 该 R 帮助消息中示例的 paste() 版本将是:writeLines(paste("msg1","msg2", sep = "\n"))。使用cat() 的版本不太好,因为您必须添加额外的"\n"cat(paste("msg1","msg2","\n", sep = "\n"))cat(paste("msg1","msg2", sep = "\n"), "\n")
【解决方案3】:

为此,我经常使用writeLines(),结合strwrap()paste() 来组合说循环值,如果我要打印当前迭代的信息。 strwrap() 处理需要的长行换行,writeLines() 意味着我不必记得在 cat() 调用的末尾添加 "\n"

> writeLines(strwrap("a very very very very long long long long long long long long string, that is too wide for the current pager width"))
a very very very very long long long long long long long long string,
that is too wide for the current pager width

这是一个使用它打印出迭代指标的示例:

for(i in 1:1000) {
    if(isTRUE(all.equal(i %% 100, 0)))
        writeLines(strwrap(paste("Iteration", i)))
    ## do something
}

给予:

> for(i in 1:1000) {
+     if(isTRUE(all.equal(i %% 100, 0)))
+         writeLines(strwrap(paste("Iteration", i)))
+     ## do something
+ }
Iteration 100
Iteration 200
Iteration 300
Iteration 400
Iteration 500
Iteration 600
Iteration 700
Iteration 800
Iteration 900
Iteration 1000

【讨论】:

  • 感谢有关 strwrap 的提示,从未听说过。显然停止和警告使用类似的方法,因此不需要手动中断。
【解决方案4】:

NULL 是“cat()”的返回值。如果省略外部的“print()”,您将看不到 NULL。

【讨论】:

  • 不确定这是否依赖于 GUI。我正在使用 RStudio。
【解决方案5】:

我认为没有必要使用print(cat())。打印消息cat() 已经足够了。这可能是您正在寻找的:

  for (j in 1:n) {
     cat("Running loop", j, "of", n, "\n")
  }

【讨论】:

    【解决方案6】:

    如果要将其分配给变量,以在 *apply 或函数 (x) 的循环中使用,请尝试以下操作:

    x<-eval(paste0(name,".y"))
    

    名称是变量,“.y”向其添加一个字符串,paste 表示要打印,eval 评估打印,

    【讨论】:

      【解决方案7】:

      我有一点不同的问题,我想连接一些 html 文本以在我的 Rmarkdown 中包装长字符串,并且从 cat() 获得相同的 NULL。只需从闪亮的包中包裹HTML() 即可解决问题。

      ```{r, results = "asis"}
      HTML(cat("<span style='white-space: pre-wrap; word-break: break-all;'>",comments,"</span>"))
      
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-28
        • 1970-01-01
        • 1970-01-01
        • 2022-06-28
        • 1970-01-01
        • 2016-07-07
        • 2011-07-13
        相关资源
        最近更新 更多