Format numericInput in R

I am trying to format the numeric input in an R shiny app just for display purpose. When I do that on load it shows the number and then the numericInput box goes blank.

library(shiny)

if(interactive()){
  shinyApp(
    ui <- fluidPage(
      mainPanel(
        numericInput("inp1", label = ("Total"), value = 11000000)
      )
    ),

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

      observe({
        updateNumericInput(session, "inp1", label = ("Total"),
                          paste("S", value = prettyNum(input$inp1, big.mark=",", scientific=FALSE)))
      })
    }
  )
}

hey @sten - it would be helpful if we had some context for this post - what type of error are you running into or what are you hoping to achieve?

My bad I just added the code. I just updated my question.

1 Like

I think this is not possible with numericInput, because the value parameter has to be numeric, but you can do it with textInput and convert the value to numeric later.

1 Like

So when I use textInput and updateTextInput I get this

image

The prefix is causing an infinite loop, try with something like this.

library(shiny)
library(stringr)
library(scales)

ui <- fluidPage(
    mainPanel(
        textInput("inp1", label = "Total", value = "110000")
    )
)

server <- function(input, output, session) {
    
    observe({
        if (!str_detect(input$inp1, "S ")) {
            updateTextInput(session, "inp1", "Total", 
                            value = number(as.numeric(input$inp1), prefix = "S ", big.mark = ","))
        }
    })
}

shinyApp(ui = ui, server = server)
1 Like

When I change the prefix S as $ it get output as $ NA.

You need to change this line

if (!str_detect(input$inp1, "\\$"))

This is because $ is a metacharacter and needs to be escaped in the regular expression with \\$

2 Likes

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.