我试图根据行的顺序在我闪亮的数据表中从最小值到最大值添加行等级。我希望行排名根据已排序的列进行更新。我最好的想法要么是使用rownames
,但随着行而变化,或者使用input$table_state$order
和saveState
找出要排序的列,然后重新计算rank
柱。我提供了一个使用iris
数据集的示例。
library(shiny)
library(DT)
library(tidyverse)
data = iris %>% distinct(Species, .keep_all = TRUE) %>%
arrange(Sepal.Length) %>%
mutate(rank = 1:3) %>%
select(rank, Species, everything())
# Define UI for application that draws a histogram
ui <- fluidPage(
DT::dataTableOutput("table")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$table = DT::renderDataTable({
DT::datatable(data,
rownames = FALSE,
options = list(
columnDefs = list(list(orderable = FALSE, targets = 0:1)),
order = list(list(2, "asc")),
ordering = TRUE
))
})
}
# Run the application
shinyApp(ui = ui, server = server)