" />

在 R 中,我可以阻止 print(cat("")) 返回 NULL 吗?为什么 cat(“foo”) 返回 foo>

发布于 2024-09-29 19:17:13 字数 192 浏览 0 评论 0原文

如果我输入,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

生来就爱笑 2024-10-06 19:17:14

我也遇到过完全相同的问题。简而言之,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 use cat() but I would suggest looking at paste().

?paste

I think it may be what you are looking for.

破晓 2024-10-06 19:17:14

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

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

I do not see the need to use print(cat()). To printing a message cat() is already sufficient. This may be what you are looking for:

  for (j in 1:n) {
     cat("Running loop", j, "of", n, "\n")
  }
岁月蹉跎了容颜 2024-10-06 19:17:14

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

> 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

For this, I often use writeLines(), in combination with strwrap(), and paste() to combine say the loop value if I'm printing out info on the current iteration. strwrap() handles wrapping long lines as required, and writeLines() means I don't have to remember to add a "\n" on the end of my cat() calls.

> 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

Here is an example using it to print out an iteration indicator:

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

Gives:

> 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
静待花开 2024-10-06 19:17:14

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

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

名称是变量,“.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:

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

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.

岛徒 2024-10-06 19:17:14

我遇到了一些不同的问题,因为我想连接一些 html 文本以将长字符串包装在我的 Rmarkdown 中,并且从 cat() 中获取相同的 NULL。只需从闪亮的包中包装 HTML() 就可以解决问题。

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



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 in HTML() from the shiny package solved the problem.

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



好倦 2024-10-06 19:17:13

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

参数:

fill:逻辑或(正)数字控制输出方式
      分成连续的行。如果为“FALSE”(默认),则仅
      打印由 '"\n"' 显式创建的换行符。
      否则,输出将被分成具有打印宽度的行
      如果“fill”为“TRUE”,则等于选项“width”,或者该值
      如果这是数字,则为“填充”。非正“填充”值
      被忽略,并带有警告。

... 和 ...

值:

 无(不可见的“NULL”)。

因此您无法阻止 print(cat(...)) 返回 NULL,因为这就是 cat 返回的内容。并且您需要显式添加换行符,例如 cat("foo\n")

All your answers are in the documentation for ?cat. The portions that answer your specific question are:

Arguments:

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.

... and ...

Value:

 None (invisible ‘NULL’).

So you can't stop print(cat(...)) from returning NULL because that's what cat returns. And you need to explicitly add newlines like cat("foo\n").

执笔绘流年 2024-10-06 19:17:13

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

NULL is the return value of "cat()". If you omit the outer "print()" you won't see the NULL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文