Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render table before it is displayed #1156

Closed
Mosk915 opened this issue Nov 21, 2024 · 2 comments · Fixed by #1158
Closed

Render table before it is displayed #1156

Mosk915 opened this issue Nov 21, 2024 · 2 comments · Fixed by #1158

Comments

@Mosk915
Copy link
Contributor

Mosk915 commented Nov 21, 2024

An issue that I keep running into quite frequently is that I need to perform proxy actions on a table in a shiny app before it ever becomes visible. This is usually when the table is in a conditional panel whose condition has never been true, or if the table is in a tab within a tabsetPanel that has never been active.

Interestingly, the replaceData action will work if it is run before the table is ever rendered, and when it is rendered it will show the correct data, but most, if not all other proxy actions will not work until the table has been rendered first.

Here is a demo app. The table is on the second tab and will not render until the second tab is activated. On the first tab are two buttons. The "Select 1st Row" button will select the first row of the table. If that button is clicked before ever activating the second tab, the first row will not be selected when going to the second tab. If it is clicked after activating the second tab, it will be selected. The "Replace Data" button will replace the data in the table, and that does work if clicked before ever activating the second tab.

library(shiny)
library(DT)

ui <- fluidPage(
  tabsetPanel(
    id = "tabs",
    tabPanel("Tab 1",
             actionButton("select", "Select 1st Row"),
             actionButton("replace", "Replace Data")),
    tabPanel("Tab 2",
             DTOutput("mt_table"))
  )
)

server <- function(input, output, session) {
  
  output$mt_table <- renderDT(mtcars)
  outputOptions(output, "mt_table", suspendWhenHidden = FALSE)
  table_proxy <- dataTableProxy("mt_table")
  
  observeEvent(input$select, {
    
    selectRows(proxy = table_proxy, selected = 1)
    
  })
  
  observeEvent(input$replace, {
    
    replaceData(proxy = table_proxy, data = rbind(mtcars, mtcars))
    
  })
  
}

shinyApp(ui, server)

It would be useful if all proxy actions would work even if the table has never been rendered. Even better would be if there was an option to render the table immediately rather than wait for the table to first become visible. This would allow for advanced modification of the table beyond just the provided proxy functions.

The only discussion I could find on this was from 2016 here. It does give a workaround which worked for that scenario. But in that case the person was wrapping the table within their own div. The workaround doesn't help when the table is in a conditional panel or a tab.

The explanation given is that due to this part, the table doesn't render until it becomes visible, i.e. if either offsetWidth or offsetHeight are 0.
https://github.com/rstudio/DT/blob/main/inst/htmlwidgets/datatables.js#L183-L186

It would be useful if a parameter could be passed through datatable that could indicate not to delay rendering the table. Call it something like lazyRender. Then in datatables.js, something like:

if ((el.offsetWidth === 0 || el.offsetHeight === 0) & data.lazyRender)

Admittedly I don't know enough to know if it's really as simple as that or if there's any other considerations to doing this. If not, perhaps there's some other way to achieve this.

@yihui
Copy link
Member

yihui commented Nov 25, 2024

t would be useful if a parameter could be passed through datatable that could indicate not to delay rendering the table. Call it something like lazyRender.

That feels like a good idea to me. Please feel free to submit a pull request. Thanks!

This was referenced Nov 25, 2024
@Mosk915
Copy link
Contributor Author

Mosk915 commented Nov 25, 2024

@yihui Thanks! I submitted a pull request. Please let me know if it's okay or if you'd like me to make any changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants