27

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?

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
David LeBauer
  • 31,011
  • 31
  • 115
  • 189
  • Could you please add some clarification? How are you running this R script (e.g. via `Rscript`, `source()`, etc)? Where do you want to print the progress (e.g. the console, a file)? – Joshua Ulrich Oct 29 '10 at 21:42
  • 3
    `cat("foo")` returns returns `foo>` because you forgot to append a newline to the end of your string. R does what you ask and prints out the string `"foo"` and then prints the prompt `>`. Try `cat("foo\n")` for example, or better, `writeLines("foo")`. I've put a few examples of the latter in my answer. – Gavin Simpson Oct 29 '10 at 21:45

7 Answers7

15

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

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 2
    Thanks for your answer - I did read the documentation before posting the question, but it was still not clear to me if there was a way to stop it from printing "NULL" or why it prints null or why the value is "None" even though it clearly returns the string provided... I guess some of my misunderstanding is more related to the theoretical than the practical. – David LeBauer Oct 29 '10 at 21:03
  • 3
    Well, the documentation can be terse and `print` and `cat` may seem like they do similar things. But `cat` _does not_ return the string provided, it _prints_ it. Subtle, but important, difference. It's also good practice to glance at the documentation pages for the functions in the "See Also" section. – Joshua Ulrich Oct 29 '10 at 21:10
  • as mentioned in response to @gunkster, I don't think I can get line breaks with paste() ... – David LeBauer Oct 29 '10 at 21:22
  • 1
    @David - you can get newlines if you insert them: `cat(paste("foo","fi","\n","\n","bar\n"))` or `writeLines(paste("foo","fi","\n","\n","bar\n"))` for example. Or see my answer for a way of using `strwrap()` and `writeLines()` to handle wrapping in a nice clean manner. – Gavin Simpson Oct 29 '10 at 21:41
  • 2
    @ucfagls: you don't need both `cat(paste(...))`. `cat` automatically separates arguments with a space... which saves some typing. ;-) – Joshua Ulrich Oct 29 '10 at 21:49
  • One-line summary: `cat()` is an alternative to **both** `print(paste(...)...)`. It does both the pasting and displaying, with optional separators and labels. – smci May 03 '14 at 22:39
14

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

d11
  • 1,344
  • 17
  • 15
7

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.

Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73
Choens
  • 1,312
  • 2
  • 14
  • 23
  • I started with the paste() function, but I couldn't get it to add line breaks, and cat() was recommended to me when I asked the r-help list about line breaks : http://tolstoy.newcastle.edu.au/R/e12/help/10/10/0104.html – David LeBauer Oct 29 '10 at 21:20
  • 2
    @David - a `paste()` version of the example in that R-help message would be: `writeLines(paste("msg1","msg2", sep = "\n"))`. A version using `cat()` is not so nice as you have to add an extra `"\n"`: `cat(paste("msg1","msg2","\n", sep = "\n"))` or `cat(paste("msg1","msg2", sep = "\n"), "\n")` – Gavin Simpson Oct 29 '10 at 21:50
6

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")
  }
MasterJedi
  • 1,618
  • 1
  • 18
  • 17
4

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
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • Thanks for tip on strwrap, never heard of that. Apparently stop and warning uses similar approach, so manual breaks are not needed. – VitoshKa Oct 30 '10 at 08:23
0

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.

Mox
  • 511
  • 5
  • 15
0

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



KeelyD
  • 161
  • 4
  • 3