Search code examples
rshinyformatwhitespacespace

R - removing white spaces using the format() function


In my Shiny app, I am trying to print a value which in reality is an amount of money.

The code at the moment is as follows:

text <- reactive({format(data()), big.mark= ",", trim = 
"TRUE")})

output$profit <- renderText({
paste("The total profit is \u00a3",text(),".")

However, there is still white spaces before and after the value that is returned from text(). How do I get rid of them?


Solution

  • In addition, we can use glue::glue

    glue("The total profit is \u00a3{text()}")
    

    -fullcode

    library(shiny)
    library(glue)
    
    options(scipen = 999)
    df1 <- data.frame(amount = c(5000, 10000, 200000))
    ui <- fluidPage(
      selectInput("amt", "amount", choices  = df1$amount),
      verbatimTextOutput(outputId = "profit")
    
    )
    server <- function(input, output) {
    
      data <- reactive(as.numeric(input$amt))
    
    text <- reactive({
       format(data(), big.mark= ",", trim = TRUE)})
    
    output$profit <- renderText({
      glue("The total profit is \u00a3{text()}")
    
    })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    -output

    enter image description here