问题
I am new to R Shinyapps, hence, my question may sound a bit stupid.
Please accept my apologies for this.
I am struggling to get the data into a table according to the required column format.
Currently, the table looks like below:
So I have 11 columns in the table, I want the columns to formatted as below:
- "Col-2 to Col-7" all having a 1 digit decimal place.
- "Col-8 to Col-10" all having 0 digit decimal place (no decimal).
- "Col-11" having 2 digit decimal place.
Any suggestions on how to do this?
I have used the below "renderTable" function to generate the table:
output$table <- renderTable(align="c", digits = 1, width="auto", na="-", subset(all, PRODUCT_NAME==as.character(input$product_choice))
[,c("Col-1", "Col-2", "Col-3", "Col-4", "Col-5", "Col-6", "Col-7", "Col-8",
paste0("Col-9 ",input$cur), paste0("Col-10 ",input$cur), "Col-11")],)
I have tried using the "Digits = 1" but it formats all the columns to 1 decimal place.
I am unable to find a way to apply the digits argument for different decimal places in separate columns. But, I need to format the columns separately I need the experts here to help me with this, please. I shall be highly thankful to you.
回答1:
Using package DT
, you can choose the columns to format by their name or number. Also, formatRound
is not the only type of formatting provided by this package (see formatCurrency
, etc.):
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput("test")
)
server <- function(input, output, session) {
output$test <- renderDataTable({
DT::datatable(iris) %>%
formatRound("Petal.Length", digits = 2, mark = "") %>%
formatRound(1, digits = 4)
})
}
shinyApp(ui, server)
回答2:
If renderTable does not give you full control over the options you need, the best thing is format columns as character using sprintf(), that will give you full control over what you can display.
Afterwards you can align them to the right as numbers, by using the align= parameter in renderTable(), which lets you specify individual alignments for each column.
来源:https://stackoverflow.com/questions/60747164/how-to-format-multiple-columns-separately-in-r-shiny