in R, can I stop print(cat(“”)) from returning NULL? and why does cat(“foo”) return foo>

后端 未结 6 1372
北恋
北恋 2021-02-18 19:48

If I enter

print(cat(\"\"))

I get

NULL

I want to use cat() to print out the progress of an R sc

相关标签:
6条回答
  • 2021-02-18 20:10

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

    0 讨论(0)
  • 2021-02-18 20:13

    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
    
    0 讨论(0)
  • 2021-02-18 20:14

    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.

    0 讨论(0)
  • 2021-02-18 20:15

    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")
      }
    
    0 讨论(0)
  • 2021-02-18 20:19

    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.

    0 讨论(0)
  • 2021-02-18 20:24

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

    0 讨论(0)
提交回复
热议问题