"EXPR must be a length 1 vector" error

4,608 views
Skip to first unread message

Chester Ismay

unread,
Jul 22, 2014, 9:44:58 PM7/22/14
to shiny-...@googlegroups.com
I am receiving many "EXPR must be a length 1 vector" errors when my Shiny app begins to run.  They soon go away after a few seconds in the running app though.

Here is one such error and the code corresponding to it in my server.R file:

Error in switch(input$probType, between = numericInput("x1", withMathJax("Enter lower value (\\(x_1\\)):"),  : 
  EXPR must be a length 1 vector

            Probability = switch(input$probType,
                                  "between" = numericInput("x1", withMathJax('Enter lower value (\\(x_1\\)):'), 1),
                                  "lowerTail" = numericInput("xFixed", withMathJax('Enter a discrete value (\\(x\\)):'), 1),
                                  "upperTail" = numericInput("xFixed", withMathJax('Enter a discrete value (\\(x\\)):'), 1),
                                  "extreme" = numericInput("x1", withMathJax('Enter lower value (\\(x_1\\)):'), 1.0),
                                  NULL),

Thanks in advance!

Chester Ismay

unread,
Jul 22, 2014, 9:53:34 PM7/22/14
to shiny-...@googlegroups.com
Here is the complete portion of code if that helps.

  output$fixedVal1 <- renderUI({
    if(input$outType != "Formulas"){
      switch(input$outType,
             PDF = numericInput("xFixed", withMathJax('Enter a discrete value (\\(x\\)):'), 1.0),
             CDF = numericInput("xFixed", withMathJax('Enter a discrete value (\\(x\\)):'), 1.0),
             Probability = switch(input$probType,
                                  "between" = numericInput("x1", withMathJax('Enter lower value (\\(x_1\\)):'), 1),
                                  "lowerTail" = numericInput("xFixed", withMathJax('Enter a discrete value (\\(x\\)):'), 1),
                                  "upperTail" = numericInput("xFixed", withMathJax('Enter a discrete value (\\(x\\)):'), 1),
                                  "extreme" = numericInput("x1", withMathJax('Enter lower value (\\(x_1\\)):'), 1.0),
                                  NULL),
             NULL
      )
    }
  })

Fereshteh Karimeddini

unread,
Jul 23, 2014, 1:29:29 PM7/23/14
to shiny-...@googlegroups.com
Chester,

I created a simple app around your code snippet, and I am not seeing the error. So it could be the way you are using this, or differences in the packages we are using.
Could you please send output of sessionInfo() so I can compare with my packages?

Thanks,
Fereshteh

Chester Ismay

unread,
Jul 23, 2014, 2:28:07 PM7/23/14
to shiny-...@googlegroups.com
Fereshteh,

Thanks for your help.  I receive the same error "EXPR must be a length 1 vector" when I run this on the Shiny Server as well.

Below is my output from sessionInfo():

R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] scales_0.2.4  ggplot2_1.0.0 shiny_0.10.0 

loaded via a namespace (and not attached):
 [1] bitops_1.0-6     caTools_1.17     colorspace_1.2-4 digest_0.6.4     grid_3.1.0       gtable_0.1.2    
 [7] htmltools_0.2.4  httpuv_1.3.0     labeling_0.2     MASS_7.3-31      munsell_0.4.2    plyr_1.8.1      
[13] proto_0.3-10     Rcpp_0.11.2      reshape2_1.4     RJSONIO_1.2-0.2  stringr_0.6.2    tools_3.1.0     
[19] xtable_1.7-3   

Chester

Winston Chang

unread,
Jul 24, 2014, 5:21:24 PM7/24/14
to Chester Ismay, shiny-discuss
This error can occur when the argument to switch is NULL, as in:
  switch(NULL, a=1, b=2)
  # Error in switch(NULL, a = 1, b = 2) : EXPR must be a length 1 vector

Adding a NULL check at the top may fix it. Something like:
  if (is.null(input$foo)) return()




--
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.
For more options, visit https://groups.google.com/d/optout.

Chester Ismay

unread,
Jul 24, 2014, 9:40:05 PM7/24/14
to shiny-...@googlegroups.com
Thanks!  That fixed the errors!

Chester
Reply all
Reply to author
Forward
0 new messages