Perfect got it. Thanks. But I did a small change .
The change is, I created a function for DT table and then called it under render function. Looks like some issue here.
Basically same concept only but calling it under function. Can you please help me here
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
DTOutput("tab")
)
server <- function(input, output, session) {
table1 <- mtcars
table1$new <- rownames(mtcars)
table1 <- table1 %>% mutate(sort_new = ifelse(new == "Hornet 4 Drive",1,ifelse(new == "Mazda RX4 Wag",2, ifelse(new == "Mazda RX4",3,
ifelse(new == "Hornet Sportabout",4,
ifelse(new == "Valiant",5,
ifelse(new == "Datsun 710",6,7))))))) %>% arrange(desc(-sort_new))
dt_table <- function(data, for_ord, ord_for){
DT::datatable(data,
options = list(
columnDefs = list(
list(orderData=for_ord,targets=ord_for),
list(visible=FALSE,targets=for_ord)
)
)
)
}
output$tab <- renderDT({
dt_table(head(table1, n = 6), for_ord = "sort_new", ord_for = "new")
})
}
shinyApp(ui, server)
But below code is working (Just changed column names by column number. Not sure what the is reason behind this.
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
DTOutput("tab")
)
server <- function(input, output, session) {
table1 <- mtcars
table1$new <- rownames(mtcars)
table1 <- table1 %>% mutate(sort_new = ifelse(new == "Hornet 4 Drive",1,ifelse(new == "Mazda RX4 Wag",2, ifelse(new == "Mazda RX4",3,
ifelse(new == "Hornet Sportabout",4,
ifelse(new == "Valiant",5,
ifelse(new == "Datsun 710",6,7))))))) %>% arrange(desc(-sort_new))
dt_table <- function(data, for_ord, ord_for){
DT::datatable(data,
options = list(
columnDefs = list(
list(orderData=for_ord,targets=ord_for),
list(visible=FALSE,targets=for_ord)
)
)
)
}
output$tab <- renderDT({
#dt_table(head(table1, n = 6), for_ord = "sort_new", ord_for = "new")
dt_table(head(table1, n = 6), for_ord = 13, ord_for = 12) ## Change here
})
}
shinyApp(ui, server)