skip to Main Content

I have a Shiny app in ShinyProxy which requires user login to access. I have a DT::datatable in the app which allows the user to re-order the columns (there are about 20-30 columns of data, so it’s a pain to re-order them). If they re-order the columns first, then use any filters/slicers, the columns are re-ordered back to the default; if they leave the app and re-open it, the columns are back to the default order.

I’d like to give the user a “save column order” ActionButton and then store those settings per user in a Redis so that when they use filters and/or leave and re-enter the app, they have their preferred column order instead of the default that I’ve set up – want to make it easy on them!!

I’ve found this site (https://rstudio.github.io/DT/shiny.html) which gets me close, but no cigar. I was thinking it may be something like input$tableId_columns_all or input$tableId_columns – however, those didn’t work/returned NA.

I’ve tried significant googling and most things I find that seem like they’d get me my answer are either Python or jQuery, which I don’t believe would solve my problem.

I’m open to suggestions which follow the ActionButton + redis method I’ve thought of or completely different idea, too! I’m really just looking to save user settings in ShinyProxy.

Thank you!!

3

Answers


  1. I don’t know how you can save/restore the order but here is how you can get the order:

    library(shiny)
    library(DT)
    
    js <- c(
      "table.on('column-reorder', function(e, settings, details){",
      "  Shiny.setInputValue('colOrder', details.mapping);",
      "});"
    )
    
    ui <- fluidPage(
      br(),
      DTOutput("tbl"),
      br(),
      verbatimTextOutput("columnsOrder")
    )
    
    server <- function(input, output, session){
    
      output[["tbl"]] <- renderDT({
        datatable(iris[1:5,], extensions = "ColReorder", 
                  callback = JS(js), 
                  options = list(
                    colReorder = TRUE
                  )
        )
      })
    
      output[["columnsOrder"]] <- renderPrint({
        input[["colOrder"]]
      })
    
    }
    
    shinyApp(ui, server)
    

    enter image description here

    Login or Signup to reply.
  2. I come really late with my solution but this could be of interest for some of you. Here, we get the final names of the column header.

    library(shiny)
    library(DT)
    
    
    newjs <- 'table.on("column-reorder", function(e, settings, details){
        var table = document.getElementById("tbl");
        var thead = table.getElementsByTagName("thead");
        var ths = thead[0].getElementsByTagName("th");
        var tableFields = [];
        for (let i = 0; i < ths.length; i++) {
            tableFields[i] = ths[i].innerHTML;            
        }
        Shiny.onInputChange("colOrder", tableFields); 
    });
    '
    
    
    ui <- fluidPage(
      br(),
      DTOutput("tbl"),
      br(),
      verbatimTextOutput("columnsOrder")
    )
    
    server <- function(input, output, session){
    
      output[["tbl"]] <- renderDT({
        datatable(iris[1:5,], extensions = "ColReorder", 
                  callback = JS(newjs), 
                  options = list(
                    colReorder = TRUE
                  )
        )
      })
    
      output[["columnsOrder"]] <- renderPrint({
        input[["colOrder"]]
      })
    
    }
    
    shinyApp(ui, server)
    
    Login or Signup to reply.
  3. Using stateSave = TRUE in the datable options might be a solution, too. Check this post.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search