Shiny "Warning in run(timeoutMs) : NAs introduced by coercion" behind the scenes.

1,600 views
Skip to first unread message

Reino

unread,
Apr 14, 2015, 5:33:24 PM4/14/15
to shiny-...@googlegroups.com
I am having in issue with shiny app that it is kicking out weird warnings and crashing. I can't seem to bug check or track down their origin because it seem to be having behind the scenes. This "warning"/error-crash does not happen on all my computers, so I am at loss as to what to do next.

On the computer that is generating the error "Warning in run(timeoutMs) : NAs introduced by coercion" all warnings have the "run(timeoutMs)" name. On the computer that runs the app with no problems, the warnings look like "Warning in eval(expr, envir, enclos) :" I would like to figure out what is different between my two computers. The only place I can find this "run(timeoutMs)" code is in the httpuv package inside of rstudio. https://github.com/rstudio/httpuv/blob/master/R/httpuv.R

The session info for the malfunctioning computer:
> sessionInfo()
R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale
:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages
:
 
[1] shinydashboard_0.3.0 DAAG_1.20            MASS_7.3-39          plyr_1.8.1          
 
[5] devtools_1.7.0       flux_0.3-0           caTools_1.17.1       aod_1.3            
 
[9] rms_4.3-0            SparseM_1.6          Hmisc_3.15-0         Formula_1.2-1      
[13] lattice_0.20-30      KMsurv_0.1-5         scales_0.2.4         digest_0.6.8        
[17] survAUC_1.0-5        survival_2.38-1      reshape2_1.4.1       heplots_1.0-12      
[21] car_2.0-25           gridExtra_0.9.1      png_0.1-7            dygraphs_0.4.3      
[25] ggplot2_1.0.1        data.table_1.9.4     shiny_0.11.1        

loaded via a
namespace (and not attached):
 
[1] acepack_1.3-3.3     bitops_1.0-6        chron_2.3-45        cluster_2.0.1      
 
[5] codetools_0.2-10    colorspace_1.2-6    foreign_0.8-63      gtable_0.1.2      
 
[9] htmltools_0.2.6     htmlwidgets_0.3.2   httpuv_1.3.2        labeling_0.3      
[13] latticeExtra_0.6-26 lme4_1.1-7          magrittr_1.5        Matrix_1.1-5      
[17] mgcv_1.8-4          mime_0.3            minqa_1.2.4         multcomp_1.4-0    
[21] munsell_0.4.2       mvtnorm_1.0-2       nlme_3.1-120        nloptr_1.0.4      
[25] nnet_7.3-9          parallel_3.1.3      pbkrtest_0.4-2      polspline_1.1.9    
[29] proto_0.3-10        quantreg_5.11       R6_2.0.1            RColorBrewer_1.1-2
[33] Rcpp_0.11.5         RJSONIO_1.3-0       rpart_4.1-9         sandwich_2.3-3    
[37] stringr_0.6.2       TH.data_1.0-6       tools_3.1.3         xtable_1.7-4      
[41] zoo_1.7-12  

The session info for the working computer:
> sessionInfo()
R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale
:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages
:
 
[1] shinydashboard_0.3.0 DAAG_1.20            MASS_7.3-39          plyr_1.8.1          
 
[5] devtools_1.7.0       flux_0.3-0           caTools_1.17.1       aod_1.3            
 
[9] rms_4.3-0            SparseM_1.6          Hmisc_3.15-0         Formula_1.2-0      
[13] lattice_0.20-30      KMsurv_0.1-5         scales_0.2.4         digest_0.6.8        
[17] survAUC_1.0-5        survival_2.38-1      reshape2_1.4.1       heplots_1.0-12      
[21] car_2.0-25           gridExtra_0.9.1      png_0.1-7            dygraphs_0.4.2      
[25] ggplot2_1.0.1        data.table_1.9.4     shiny_0.11.1        

loaded via a
namespace (and not attached):
 
[1] acepack_1.3-3.3     bitops_1.0-6        chron_2.3-45        cluster_2.0.1      
 
[5] codetools_0.2-10    colorspace_1.2-6    foreign_0.8-63      gtable_0.1.2      
 
[9] htmltools_0.2.6     htmlwidgets_0.3.2   httpuv_1.3.2        labeling_0.3      
[13] latticeExtra_0.6-26 lme4_1.1-7          magrittr_1.5        Matrix_1.1-5      
[17] mgcv_1.8-4          mime_0.3            minqa_1.2.4         multcomp_1.4-0    
[21] munsell_0.4.2       mvtnorm_1.0-2       nlme_3.1-120        nloptr_1.0.4      
[25] nnet_7.3-9          parallel_3.1.3      pbkrtest_0.4-2      polspline_1.1.9    
[29] proto_0.3-10        quantreg_5.11       R6_2.0.1            RColorBrewer_1.1-2
[33] Rcpp_0.11.5         RJSONIO_1.3-0       rpart_4.1-9         sandwich_2.3-3    
[37] stringr_0.6.2       TH.data_1.0-6       tools_3.1.3         xtable_1.7-4      
[41] zoo_1.7-12        

Any help would be greatly appreciated,
Reino

Joe Cheng

unread,
Apr 14, 2015, 6:46:05 PM4/14/15
to Reino, shiny-...@googlegroups.com
Can you try setting these options and trying again? Hopefully it will drop your R session into a browser prompt when the warning is encountered.

options(warn=2, error=browser, shiny.error=browser)

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/1f538c76-6e19-4500-b05b-0fded37af997%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reino

unread,
Apr 15, 2015, 8:54:29 AM4/15/15
to shiny-...@googlegroups.com, reino...@gmail.com
Thank you for your response.

Last night, I was able to isolate there the warning was coming from. Strangely, it was from the all.is.numeric() function from the Hmisc package. Inside it, the function performs an is.na(as.numeric(x)) where if the value x can't be coerced into a numeric value, the function returns a FALSE. Funny enough, on malfunctioning computer, this warning pops out constantly with the "run(timeoutMs)" label, where as the windows 8 stable machine does not and throws warnings (when I force it to) with a "eval(expr, envir, enclos)" label. These two computers are running the exact same app, with the exact same options.

So after rectifying the warning — by rewriting the function — the app still intermittently crashes on some computers.

Yihui Xie

unread,
Apr 15, 2015, 11:02:45 AM4/15/15
to Reino, shiny-discuss
I guess you can try debug(httpuv::service) then run your app, and R
will enter the debug mode. Then you can check the value of timeoutMs
and see what is possibly wrong with this value. You may also print the
value of as.numeric(Sys.time()) * 1000 to help us debug the issue.

Regards,
Yihui
> https://groups.google.com/d/msgid/shiny-discuss/93e6a4b5-3d3a-4e73-9144-2f3d10ba4113%40googlegroups.com.

Reino

unread,
Apr 15, 2015, 1:01:03 PM4/15/15
to shiny-...@googlegroups.com, reino...@gmail.com
Thanks Yihui,

I didn't know about debug(httpuv::service) as a tool. Pretty cool. I will have to use it more often on some of my sourced functions. The little red dot doesn't always work for me.

So I sorted out the "NAs introduced by coercion" warning last night be rewritting all.is.numeric with a suppresswarning. It is weird that the two nearly identical computers handled that differently.

I just tested the fix on the crashing computer, and it seems to work. Weird how such a tiny warning would have such a whole effect.

Joe Cheng

unread,
Apr 15, 2015, 1:14:05 PM4/15/15
to Reino, shiny-...@googlegroups.com
Do you have options(warn) set on one of the computers? I don't know why else it'd pose such a problem if suppressWarnings fixes it.

Yihui Xie

unread,
Apr 15, 2015, 1:16:36 PM4/15/15
to Reino, shiny-discuss
It may not be obvious to you that run(timeoutMs) is a call inside
httpuv::service, which is called by shiny. That is why I suggested you
debug httpuv::service, and see what exactly the value of timeoutMs is
(which you did not tell us), and why R failed to coerce it to a
numeric value. I think it will be nice if we can figure out the root
cause instead of sweeping the issue under the carpet by
suppressWarnings().

Regards,
Yihui

Joe Cheng

unread,
Apr 15, 2015, 1:28:36 PM4/15/15
to Yihui Xie, Reino, shiny-discuss
I suspect run(timeoutMs) isn't actually relevant here, but just R/httpuv/shiny printing out the wrong part of the call stack.

Reino, where was the code that was calling all.is.numeric? In your Shiny Server function directly? In a reactive, or output? At the top of server.R?

Reino

unread,
Apr 15, 2015, 4:13:49 PM4/15/15
to shiny-...@googlegroups.com, yi...@rstudio.com, reino...@gmail.com
Yihui,

 “I suggested you debug httpuv::service, and see what exactly the value of timeoutMs is (which you did not tell us), and why R failed to coerce it to a numeric value. “

I had to undo my “fix” so that I could show you what happens Yihui. This is the verbatim code what is the culprit. It is pretty much verbatim from the function all.is.numeric {Hmisc}. I use it inside of another function.

all.is.numeric2 <- function (x, what = c("test", "vector"), extras = c(".", "NA")) {
  what
<- match.arg(what)
  old
<- options(warn = -1)
  on
.exit(options(old))
  x
<- sub("[[:space:]]+$", "", x)
  x
<- sub("^[[:space:]]+", "", x)
  xs
<- x[x %nin% c("", extras)]
  # suppressWarnings
(isnum <- !any(is.na(as.numeric(xs))))
  isnum <- !any(is.na(as.numeric(xs)))  if (what == "test")
    isnum
 
else if (isnum)
   
as.numeric(x)
 
else x
}

It only crashes the app when suppressWarnings() is not being used. Crashes are intermitting, and varied in what exactly crashes. I have gotten:

  1) Error in eval(substitute(expr), envir, enclos) :  cncompatible lengths for set aesthetics: fontface, label
  2) I have had it just crash.
  3) Sometimes it kicks out 50 NA coercion warnings and then turns off.
  4) Sometimes the exact same code produces no warnings at all, and runs fine.

When I am able to catch the warning, it shows that it was provoked by strings of characters going into the as.numeric() function as intended.

When I run it with debug(httpuv::service) in the pre-server function script, the bug tracker drops into the code below and keeps loading 100, and never stops. It seems impossible to advances the webapp when I press continue a million times.

“service” function.
function (timeoutMs = ifelse(interactive(), 100, 1000))
{
    run(timeoutMs)
}

I think it will be nice if we can figure out the root cause instead of sweeping the issue under the carpet by suppressWarnings().
 
I am completely agree.

Joe,

“Do you have options(warn) set on one of the computers? I don't know why else it'd pose such a problem if suppressWarnings fixes it.”

I have options(warn = 1) embedded in the app pre shinyServer().  I never change it.

“I suspect run(timeoutMs) isn't actually relevant here, but just R/httpuv/shiny printing out the wrong part of the call stack.”

I tend to agree with you Joe. I have run options(shiny.trace = TRUE) like you showed me before:

https://groups.google.com/forum/#!searchin/shiny-discuss/Reino/shiny-discuss/7y5rtthE3k0/WbPXmvvjHQ0J

It doesn’t seem show any useful information. I have to admit I am not great at reading the output.

“Reino, where was the code that was calling all.is.numeric? In your Shiny Server function directly? In a reactive, or output? At the top of server.R?”

All over the place Joe. Lol

The function is defined:
source(source_file, local = TRUE)
within source_file:
            all.is.numeric2 <- function(input){…
                                 isnum <-!any(is.na(as.numeric(xs)))
                               … }
            hold <- function(input){…
                        all.is.numeric2(“sometimes a string”)
                    …}
shinyServer(function(input, output, clientData, session){
  obs_fun <- observe({
              …
              hold(…)
              …
  })
  reac_fun <- reactive({
              …
              hold(…)
              …
  })
}

 Thank you guys for looking into this for me. I would love to know why this kind of stuff is happening.

Reino

Yihui Xie

unread,
Apr 15, 2015, 5:01:56 PM4/15/15
to Reino, shiny-discuss
Okay, the use of suppressWarning() in that case is completely
legitimate in my eyes. FWIW, Frank (author of Hmisc) has considered
suppressing the warning by options(warn = -1), but that is actually
not the appropriate way to suppress warnings for a particular
expression in R: suppressWarnings() should be the correct way. There
is a subtle difference between the two ways
(https://github.com/yihui/knitr/issues/610). I suggest you write to
Frank, and recommend him to use suppressWarnings() instead of
options(warn = -1).

I do not see significant differences between the two R sessions:
https://www.diffchecker.com/vmzd3js8 I do not understand why the
warning messages can be different on the two computers.

Regards,
Yihui

Reino

unread,
Apr 15, 2015, 9:27:19 PM4/15/15
to shiny-...@googlegroups.com, reino...@gmail.com
" I suggest you write to Frank, and recommend him to use suppressWarnings() instead of
options(warn = -1)."

Am comfortable applying the suppressWarning() myself. If I really wanted to fix the problem, I would make a new function in Rcpp. Modeling it off of as.numeric() which is just an method for .Primitive("as.double").

From names.c:
/* Type coercion */
{"as.double", do_ascharacter, 2, 1, -1, {PP_FUNCALL, PREC_FN, 0}},

From coerce.c:
/* the "ascharacter" name is a historical anomaly: as.character used to be the
 * only primitive;  now, all these ops are : */

SEXP attribute_hidden do_ascharacter
(SEXP call, SEXP op, SEXP args, SEXP rho)
{
...
   
case 2:
    name
= "as.double"; type = REALSXP; break;
...
}
/*Which gets plopped into isna later..*/
SEXP attribute_hidden do_isna
(SEXP call, SEXP op, SEXP args, SEXP rho)
{...
   
case REALSXP:
   
for (i = 0; i < n; i++)
        LOGICAL
(ans)[i] = ISNAN(REAL(x)[i]);
...}

So it is really just a quick for loop which returns a logical array.

If I want this process to be optimized, I would just make a simple vector scan in c++.
Otherwise, I think a regular expression using grepl would be better.


I do not see significant differences between the two R sessions: https://www.diffchecker.com/vmzd3js8 I do not understand why the warning messages can be different on the two computers.

Dude, that compare site is amazing! Thanks for posting it. But yeah, the point is that that change should not have done anything important. I am interested to find out how the call stack could be malfunctioning.
Reply all
Reply to author
Forward
0 new messages