0

Good day all,

I am trying to count the number of characters in a decimal with trailing zeros when my micrometer sent its numeric input to my shiny app. My count should include both the . and any trailing zeros.

Example: 0.500 should have a total of 5 characters and 0.600000 should have 8.

Initially, I tried converting it to character:

nchar(as.character(0.500))
[1] 3
as.character(0.500)
[1] "0.5"

Then I tried using paste0 but it won't retain the trailing zeros

paste0("'",0.500,"'")
[1] "'0.5'"

Here is a reproducible example of a shiny app that works similarly to my existing application:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
   dashboardHeader(
     title = "Test"
   ),
      dashboardSidebar(collapsed = T),
      dashboardBody(
         fluidRow(
            column(width = 4,
         numericInput(inputId = "data_input",
                      label = "Data",
                      value = 0.500
                      )
  ),
  
  column(width = 8,
         textOutput("data_output")
     )
   )
  )
)

server <- function (input, output, session) {

  output$data_output <- renderPrint("Does not have 5 characters!")

  observe({
    if(nchar(input$data_input) > 4){
     output$data_output <- renderPrint({
    "Yah! 5 characters now!"
   })
     } else {
       output$data_output <- renderPrint("Does not have 5 characters!")
     }
    })
}
shinyApp(ui, server)

I have tried looking for a solution for the past 2 hours but I can't seem to locate any. Thanks for your help!

Dwight
  • 124
  • 2
  • 12
  • how bout this? `paste0(0.5, "00")` – Adam Quek Jul 15 '22 at 07:44
  • hmm, that is just adding 2 zeros to a decimal. What I want to do is count every trailing zeros at the end. 0.600000000 should return 11 characters (including .) – Dwight Jul 15 '22 at 07:47
  • Do you mean `nchar("0.500")`? – Jon Spring Jul 15 '22 at 07:48
  • @JonSpring my input comes in numeric. So 0.500 is a numeric value. I will need to convert that to a character but doing so remove all trailing zeros. – Dwight Jul 15 '22 at 07:49
  • 2
    How is 0.500 meant to be interpreted differently than 0.5? Can you share a real example, e.g. with `dput`? – Jon Spring Jul 15 '22 at 07:50
  • @JonSpring we have a micrometer that generate an input in 3 decimal places. So it generate 0.500 instead of 0.5. We have an observe statement in our app that detects once the user input in 5 character, the data will be automatically submitted to our db. – Dwight Jul 15 '22 at 07:54
  • 3
    For us to progress here, please share an example using `dput`. e.g. if you have a vector, `dput(head(MY_VECTOR))` or `dput(head(MY_DATA_FRAME$INTERESTING_COLUMN))` and paste the output into your question. This will give us insight about what format your data is in and test potential solutions. – Jon Spring Jul 15 '22 at 07:58
  • How can you input numeric values with trailing 0s in R? – Sotos Jul 15 '22 at 08:25
  • 1
    As others have implied, your data needs to be stored as character rather than numberic for this to be possible. The you can simply do something like `nchar("0.500")` – Miff Jul 15 '22 at 08:28
  • @Sotos you can key trailing zeros in the numericInput in R shiny. What I want to do is detect 5 characters... – Dwight Jul 15 '22 at 08:29
  • 1
    Oh it's for shiny. You should mention that in your question and also tag it – Sotos Jul 15 '22 at 08:33
  • 1
    The issue seems to be that R strips trailing zeros from numerical values when the value is assigned, i.e. `x <- 0.500`, `x` gives "0.5". So, can you read in the inputs as characters? `nchar(c("0.5", "0.05", "0.050", "0.5000", "800", "0.004"))` `3 4 5 6 3 5` – Tech Commodities Jul 15 '22 at 08:44
  • @TechCommodities I won't be able to read the input as characters. I tried switching from numericInput() to textInput in hope that it will convert all input into texts but it still convert 0.500 to 0.5. – Dwight Jul 15 '22 at 08:46
  • @Dwight Please share the code where you using a textinput rather than numeric and still had it remove zeros. That doesn't seem possible. The character value should have all zeros. When you convert to numeric, all training zeros are lost. Because as a number, it would have an infinite amount of trailing zeros. Floating point numbers don't have a fixed number of digits, only strings have a fixed number of characters. – MrFlick Jul 15 '22 at 13:06

1 Answers1

1

You need to use textInput instead of numericInput. You need to quote "0.500" as the start value too, as it would otherwise be converted to 0.5 as you already know by your experiments (and by the comments of e.g. @Tech Commodities).

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
    dashboardHeader(
        title = "Test"
    ),
    dashboardSidebar(collapsed = T),
    dashboardBody(
        fluidRow(
            column(width = 4,
                   textInput(inputId = "data_input",
                                label = "Data",
                                value = "0.500"
                   )
            ),
            
            column(width = 8,
                   textOutput("data_output")
            )
        )
    )
)

server <- function (input, output, session) {
    
    output$data_output <- renderPrint("Does not have 5 characters!")
    
    observe({
        if(nchar(input$data_input) > 4){
            output$data_output <- renderPrint({
                "Yah! 5 characters now!"
            })
        } else {
            output$data_output <- renderPrint("Does not have 5 characters!")
        }
    })
}
shinyApp(ui, server)

Output:

enter image description here

harre
  • 7,081
  • 2
  • 16
  • 28