GithubHelp home page GithubHelp logo

Comments (4)

stla avatar stla commented on June 9, 2024

From the documentation:

When you replace the data in an existing table, please make sure the new data has the same number of columns as the current data.

So if you start with a NULL dataset, it is expected that replaceData will not work.

from dt.

rfgoldberg avatar rfgoldberg commented on June 9, 2024

That's what option 1. is supposed to simulate. If you populate the proxy with a dataset with no rows but that has the correct number of columns, the filters still don't work when data is updated and elongated.

from dt.

stla avatar stla commented on June 9, 2024

I think the reason can also be found in ?replaceData:

When you have enabled column filters, you should also make sure the attributes of every column remain the same, e.g. factor columns should have the same or fewer levels, and numeric columns should have the same or smaller range, otherwise the filters may never be able to reach certain rows in the data, unless you explicitly update the filters with updateFilters().

from dt.

rfgoldberg avatar rfgoldberg commented on June 9, 2024

Thank you so much for this clarification. I believe updateFilters is what I'm looking for, but I've run into another issue with it though. I've modified the reprex above to include the modifications you suggested and with a wider dataset as is applicable to my use case. If you run the reprex, you'll notice very long processing time between when the button to populate the data is clicked and when the table actually renders. It also takes longer for just the empty table to show up. I only observe this behavior when the top filter is implemented. Once top filter is removed, the processing speed increases dramatically to populate the DT. Is this something that should be faster? On my actual shiny dashboard, it results in about 12-15 seconds of nothing happening, for a table of 3 row x 189 cols.

## added to load wide dataset (143 variables)
dat <- chickwts %>%
  bind_rows(chickwts) %>%
  mutate(name = paste0("col",row_number())) %>%
  pivot_wider(id_cols = feed, names_from = name, values_from = weight)





shinyApp(
  
  ui = fluidPage(
    actionButton('showdata',"Show Data"),
    actionButton('prev_five', 'Previous Cols'),
    actionButton('next_five', 'Next Cols'),
    DTOutput('tbl')),
  
  
  server = function(input, output) {
    
    
    
    cols <- reactiveValues()   
    cols$showing <- 1:3    
    
    # currently relies on observer to populate data
    observeEvent(input$showdata,{
      cols$df <- dat 
    })
    
    # 1. use this to observe an empty dataset fed to proxy first
    observe({
      cols$df <- dat %>% filter(col1 == "test")
    })
    
    # 2. use this to observe a filled dataset fed to proxy first 
    # change matrix to only one or two rows to see the difference in tp filters that appear in the DT
    # observe({
    #   # cols$df <- iris %>% filter(Species == "x")
    #   cols$df <- data.frame(matrix(1:10,nrow = 2,ncol = 5)) |>
    #     `colnames<-` (c(names(iris)))
    # })
    
    
    #show the next five columns 
    observeEvent(input$next_five, {
      #stop when the last column is displayed
      if(cols$showing[[length(cols$showing)]] < length(cols$df)) {
        hideCols(proxy, cols$showing, reset = FALSE) #hide displayed cols
        cols$showing <- cols$showing + 2
        showCols(proxy, cols$showing, reset = FALSE) #show the next five 
      } 
    })
    
    #similar mechanism but reversed to show the previous cols
    observeEvent(input$prev_five, {
      #stop when the first column is displayed
      if(cols$showing[[1]] > 1) {
        hideCols(proxy, cols$showing, reset = FALSE) #hide displayed cols
        cols$showing <- cols$showing - 2
        showCols(proxy, cols$showing, reset = FALSE) #show previous five
      } 
    })
    
    
    output$tbl = renderDT({
      datatable(isolate(cols$df),filter = "top", # <- if this is commented out, this goes much more quickly
                options = list(
                  columnDefs = list(list(visible = FALSE, targets = 1:length(isolate(cols$df)))), #hide all columns
                  scrollX = TRUE)  #for when many columns are visible
      )
    })
    
    
    proxy <- dataTableProxy('tbl')
    observe({
      DT::replaceData(proxy, cols$df, resetPaging = FALSE)
      DT::updateFilters(proxy,cols$df)
    })
    showCols(proxy, 1:3, reset = FALSE) #show the first five cols (because the columns are now all hidden)
  
  }
)

from dt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.