Whi is 'cat' sometimes ignored?

I noticed that sometimes the command 'cat' is ignored. Nothing is printed, even when 'cat' is followed by 'flush.console()'. I tried hard to create a reproducible example. The problem happens every single time I run certain functions, it is not intermittent. However, those functions are complex and depend on running other functions before them, so I cannot post the code here.

I tried four things trying to reproduce the problem:

  1. Put the call to 'cat' inside a function that is called by another function.
  2. Put the call to 'cat' inside a branch of an 'if' statement.
  3. Initiate a parallelized calculation immediately after the call to 'cat' because cat is ignored in similar circumstances in a bigger function I wrote.
  4. Make the function being called return something a bit complicated, not just a scalar.

It did not work.

I see this problem quite frequently, and I believe other R programmers have seen it. I am running RStudio 1.4.1717 and the problem happens both under Windows 10 Pro and Kubuntu Linux.

Here is the code I used to unsuccessfully try to reproduce the problem:

library(pbmcapply)

my.list <- as.list(1:1e03)
my.fun <- function(x) {
  y <- x^2 + 2
  data.frame(Y = rep(y, 1000), Z = 1L)
}

some.value <- 1

ignore.fun <- function(n.cores) {
  if (some.value == 1) {
    cat("Why am I not being ignored?\n")
    flush.console()
    
    pbmclapply(my.list, my.fun,
               mc.style = "ETA", mc.substyle = NA,
               mc.cores = n.cores,
               ignore.interactive = TRUE,
               mc.preschedule = TRUE, mc.set.seed = TRUE,
               mc.cleanup = TRUE, mc.allow.recursive = TRUE) -> value
    } else {
      value <- NULL
    }
    
    value
}

ignore2.fun <- function(x) {ignore.fun(x)}

ignore2.fun(8L) -> ifun

# Why am I not being ignored?
# |=======================| 100%, Elapsed 00:01

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.