rstudio / promises

A promise library for R
https://rstudio.github.io/promises
Other
198 stars 19 forks source link

No longer able to hide columns in DT based on column name when leveraging promises object #17

Closed mmgledhill closed 6 years ago

mmgledhill commented 6 years ago

No longer able to hide columns based on column name from promises object with DT@async

See question on stackoverflow

---
title: "Test DT Column Visibility"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
---
    ```{r setup}
    library(flexdashboard)
    require(DT)
    library(dplyr)
    library(future)
    library(promises)
    plan(multiprocess)
    library(shiny)   

Row {data-height=650}

Table

    all_data_async<-reactive({
      future({mtcars})

    })

    hide.cols<-c("hp","drat","qsec")

    #promise based on another promise
    names_all_data_async<-reactive({
      all_data_async() %...>% names() %...>% match(hide.cols,.)%...T>% print() %...>% -1

    })

    DT::renderDataTable({ all_data_async()},
                        options=list(#to hide columns by default
                                     columnDefs=list(list(visible=FALSE, 
                                                          targets=names_all_data_async())),
                                     dom="Bfrtip",
                                     buttons=list('colvis')
                        ))

`
jcheng5 commented 6 years ago

First, you'll need to install both rstudio/DT@async and ramnathv/htmlwidgets@async. (Sorry, async gets its tendrils into everything; it'll be easier when all the relevant packages have gone to CRAN.)

Then, try rewriting the DT::renderDataTable like so:

DT::renderDataTable({
  promise_all(data = all_data_async(), cols = names_all_data_async()) %...>%
    with({
      datatable(data, options = list(
        columnDefs = list(
          list(
            visible = FALSE, 
            targets = cols
          )
        ),
        dom="Bfrtip",
        buttons=list('colvis')          
      ))
    })
})

See https://rstudio.github.io/promises/articles/combining.html for an explanation of the promise_all(...) %...>% with({ ... }) idiom.

The DT::renderDataTable function's options can't contain promises. But, you can rewrite the body of the renderDataTable to take the resolved values of the promises, and make a regular options object, which fortunately can also be passed to DT::datatable.