1

I was interested in converting my numericInputs into autonumericInputs from the shinywidgets package. My primary issue is that even when the default value of autonumericInput is NA/NULL/"", the output still reads as 0, and counts it as having 1 character. From a server side, I'd like to say one of the following: if(is.null(x) | is.na(x) | x == "" | nchar(x) == 0), then another object will not run.

For some numericInputs this isn't an issue because I can just say it needs to be greater than 0. However a 0 is acceptable for some inputs, and I want to force the input to be filled in. I suppose I could default the input to "-1" and then say greater than or equal to 0, requiring at least some interaction with the input, but that seems messy to me.

This is an example with regular numericInput on the first column, and autonumericInput on the second column. The text below each input should not show if the field is blank.

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  column(width = 2,
  numericInput("Test", "Numeric Input Test", value = NULL),
  textOutput("Test2")
  ),
  column(width = 2,
  autonumericInput("Test1", "AutoNumeric Input Test", value = NULL),
  textOutput("Test3")
  )
)

server <- function(input, output, session) {
  
  output$Test2<-renderText({
    req(input$Test, nchar(input$Test) > 0, !is.na(input$Test), !is.null(input$Test))
    paste0("Numeric Input reads as: ",input$Test,". Number of characters: ",nchar(input$Test))
    })
  
  output$Test3<-renderText({
    req(input$Test1, nchar(input$Test1) > 0, !is.na(input$Test1), !is.null(input$Test1))
    paste0("Autonumeric Input reads as: ",input$Test1,". Number of characters: ",nchar(input$Test1))
  })
  
  
}
shinyApp(ui, server)

I have not found a way to handle these, and I was hoping somebody had an idea. Perhaps this just needs to be a feature request or issue on the package github: https://github.com/dreamRs/shinyWidgets/ I've read through the documentation and at least I'm not seeing any functions that I think would solve it: https://search.r-project.org/CRAN/refmans/shinyWidgets/html/autonumericInput.html

Why I'd like to switch to autonumericInput is because I like the greater control, simplifying user inputs. Limiting accepted values, always showing decimal places, limited points accepted after a decimal, showing symbols like %.

Perhaps this is an out of date package problem? I'm hoping that's not the case, else I need to update packages of several machines some of which don't have internet access, here is my sessionInfo():

Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

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

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

other attached packages:
[1] shinyWidgets_0.6.0 shiny_1.6.0       

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        withr_2.4.2       digest_0.6.27    
 [4] later_1.2.0       mime_0.7          R6_2.4.0         
 [7] jsonlite_1.6      lifecycle_1.0.0   xtable_1.8-4     
[10] magrittr_1.5      cli_2.5.0         cachem_1.0.4     
[13] rlang_0.4.11      promises_1.2.0.1  jquerylib_0.1.4  
[16] bslib_0.2.4       ellipsis_0.3.2    tools_3.6.1      
[19] httpuv_1.6.1      fastmap_1.1.0     compiler_3.6.1   
[22] sessioninfo_1.1.1 htmltools_0.5.1.1 sass_0.3.1  

I don't know if this is related, but I think shinywidgets uses javascript, and this stack listing mentions javascript reading empty lines as 0: Node.js + Lazy: how to prevent an empty line being read as "0"? All else fails, I'll just stick with regular numericInput. A pre-emptive thanks for anyone reading this!

Silentdevildoll
  • 1,187
  • 1
  • 6
  • 12

0 Answers0