Format the numericInput in R with rounding but not change the actural value

shiny
r
#1

I am wondering if it is possible to round the number to the input field of the numericInput but not change the true value. The ideal behavior of the input field would be similar to the cells in Excel spreadsheet as the users can format the cell value appearance, but the underlying numeric value is the original form. I found this Stack Overflow post "https://stackoverflow.com/q/55643980/7669809" describing the same question. For now, all the answers in the Stack Overflow post suggested that it is necessary to create two values. One is rounded, while the other one is not. Some people suggested that it should be a feature request to the Shiny team at RStudio. I would be grateful if someone can provide some answers or insights for this question.

0 Likes

#2

You can change the number of digits shown when printing numbers with the digits option.

getOption("digits")
# [1] 7
pi
# [1] 3.141593
options(digits = 2)
pi
# [1] 3.1
0 Likes

#3

Thanks for your response. But adding options(digits = 2) seems not affecting the input value of the numericInput.

Please see the following R script as an example. When running it as a Shiny app, I would like to see the numericInput "C"'s value as rounded such as 0.67, but the actual value is still 0.66666....

   library(shiny)

   # Define UI
      ui <- fluidPage(
      numericInput(inputId = "A", "Number A", value = 2),
      numericInput(inputId = "B", "Number B", value = 3),
      numericInput(inputId = "C", "Number C [A/B]", value = 1)
    )

    # Server logic
    server <- function(input, output, session){
      observe({
        req(input$A, input$B)
        num_C <- input$A/input$B
        updateNumericInput(
          inputId = "C",
          session = session,
          value = num_C
        )
      })
    }

# Complete app with UI and server components
shinyApp(ui, server)
0 Likes