[Rd] [patch] Fix n arg in mclapply call to ngettext

Scott Kostyshak skostysh at princeton.edu
Sun Jun 29 16:03:52 CEST 2014


Regarding the following code,

warning(sprintf(ngettext(has.errors,
      "scheduled core %s encountered error in user code, all values of
the job will be affected",
      "scheduled cores %s encountered errors in user code, all values
of the jobs will be affected"),
    paste(has.errors, collapse = ", ")),
  domain = NA)

has.errors is a vector whose elements are the cores that have encountered
errors. The plural message thus appears if the first element of has.errors is
greater than one and is singular otherwise. What we want is for the plural
message to be given if more than one core encountered errors. Changing the n
arg of ngettext from has.errors to length(has.errors) leads to the correct
messages.

Attached is a patch.

More details for completeness:

I've reproduced this on 3.1.0 and r66050.

Below is an example that leads to bad output sometimes (depending on
the order in which the cores finish).
library(parallel)
options(mc.cores = 4)
abc <- mclapply(2:5, FUN = function(x) stopifnot(x >= 4))
# Warning message:
# In mclapply(2:5, FUN = function(x) { :
#   scheduled core 1, 2 encountered error in user code, all values of
the job will be affected

# if a core with number great than 1 has the only error, then an
incorrect message is shown:
library(parallel)
options(mc.cores = 4)
abc <- mclapply(2:5, FUN = function(x) stopifnot(x <= 4))
# Warning message:
# In mclapply(2:5, FUN = function(x) { :
#  scheduled cores 4 encountered errors in user code, all values of
the jobs will be affected

> sessionInfo()
R Under development (unstable) (2014-06-29 r66050)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

Scott


--
Scott Kostyshak
Economics PhD Candidate
Princeton University
-------------- next part --------------
Index: src/library/parallel/R/unix/mclapply.R
===================================================================
--- src/library/parallel/R/unix/mclapply.R	(revision 66050)
+++ src/library/parallel/R/unix/mclapply.R	(working copy)
@@ -172,7 +172,7 @@
         if (length(has.errors) == cores)
             warning("all scheduled cores encountered errors in user code")
         else
-            warning(sprintf(ngettext(has.errors,
+            warning(sprintf(ngettext(length(has.errors),
                                      "scheduled core %s encountered error in user code, all values of the job will be affected",
                                      "scheduled cores %s encountered errors in user code, all values of the jobs will be affected"),
                             paste(has.errors, collapse = ", ")),


More information about the R-devel mailing list