in R, can I stop print(cat(“”)) from returning NUL

2019-04-03 18:09发布

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?

6条回答
看我几分像从前
2楼-- · 2019-04-03 18:49

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").

查看更多
我想做一个坏孩纸
3楼-- · 2019-04-03 18:53

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.

查看更多
干净又极端
4楼-- · 2019-04-03 18:57

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.

查看更多
我命由我不由天
5楼-- · 2019-04-03 19:08

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
查看更多
Deceive 欺骗
6楼-- · 2019-04-03 19:09

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")
  }
查看更多
做个烂人
7楼-- · 2019-04-03 19:12

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

查看更多
登录 后发表回答