user9750056
user9750056

Reputation:

Right-justification of formatted figures in data frame column in r shiny output table


Consider the following R Shiny code that outputs a table of values:

library(shiny)

# Define UI ----
ui <- fluidPage(

  fluidRow(column(12, numericInput("someVar1", "Var1", value = 30000)),
           column(12, numericInput("someVar2", "Var2", value = 584000)),

  tableOutput("myDataTable")

  )
)

# Define server logic ----
server <- function(input, output) {

  myDataTable <- reactive({
    myDataTable <- data.frame("Var1" = formatC(c(input$someVar1, input$someVar2), format = "d", big.mark = ","))
    return(myDataTable)
  })

  output$myDataTable <- renderTable(myDataTable())
}

# Run the app ----
shinyApp(ui = ui, server = server)

There appear to be a number of ways of formatting the data in the output table to achieve comma-separated values with or without decimal places using functions such as 'formatC', 'format', 'prettyNum', etc.

However, all of these appear to left-justify the results (after much experimentation). How can I format the numeric data in (all of) the columns of a data frame that is output to an R Shiny app with specific formatting but still maintain right-justification?

For example, instead of:

1,000.5
34,000.00

...I wish to have:

 1,000
34,000

Upvotes: 2

Views: 626

Answers (1)

Katia
Katia

Reputation: 3914

There is "align" option in the renderTable() function you are using. Please see Rshiny documentation: https://shiny.rstudio.com/reference/shiny/1.0.5/renderTable.html

output$myDataTable <- renderTable(myDataTable(), align="r")

enter image description here

Upvotes: 1

Related Questions