DT
DT copied to clipboard
Style choices get lost with `if` rule in `renderDataTable()` (Shiny)
Hi everyone,
I just noticed a strange behavior, possibly a (minor) bug. It seems that having an if
rule within renderDataTable()
, can make the table forget the style that was applied to it.
Reprex
library(shiny)
library(DT)
library(shinyWidgets)
data(iris)
ui <- fluidPage(
tagList(
shinyWidgets::materialSwitch(
inputId = "switch",
label = "Show table:",
value = TRUE,
inline = TRUE
),
uiOutput("content")
)
)
server <- function(input, output) {
output[["content"]] <- renderUI({
if ( input[["switch"]] == TRUE ) {
dataTableOutput("table")
} else {
textOutput("no_data")
}
})
output[["table"]] <- renderDataTable({
if ( input[["switch"]] == TRUE ) {
DT::datatable(
iris,
autoHideNavigation = TRUE,
class = "stripe table-bordered table-condensed",
escape = FALSE,
filter = list(position = "top", clear = TRUE),
rownames = FALSE,
selection = "single",
style = "bootstrap",
options = list(
dom = "frtlip",
pageLength = 10,
scrollX = TRUE
)
)
}
})
output[["no_data"]] <- renderText("No data.")
}
shinyApp(ui = ui, server = server)
There is a button that controls whether the table (that uses the bootstrap
style) or an alternative message is shown. Now, in this example, even though not reflecting any realistic setting, I have also added an if
rule that, again, checks the status of the switch. When the app is launched, the table is shown with the correct style (see screenshot below).
However, after using the switch to display the alternative message, and then switching back to the table, the table is shown again, however without the bootstrap
style (see screenshot).
When looking at the HTML code, I noticed that the dt-bootstrap
is missing in the list of classes of the respective div
container.
Before using the switch:
After using the switch:
Workaround
In my actual scenario, I used the if
statement to make some format checks on the input data. The workaround is to move those checks from the renderDataTable()
call to the renderUI()
call. In my case, that was no problem, but it might be inconvenient in other scenarios. That's why I decided to report this behavior here.
If you think this is important enough, maybe somebody could have a look at what's going on here.
Thank you!
Session info
> xfun::session_info()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.6, RStudio 1.2.1335
Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
Package version:
askpass_1.1 base64enc_0.1.3 BH_1.72.0.3
commonmark_1.7 compiler_3.6.3 crayon_1.3.4
crosstalk_1.1.0.1 curl_4.3 digest_0.6.25
DT_0.14.3 fastmap_1.0.1 glue_1.4.1
graphics_3.6.3 grDevices_3.6.3 htmltools_0.5.0
htmlwidgets_1.5.1 httpuv_1.5.4 jsonlite_1.7.0
later_1.1.0.1 lazyeval_0.2.2 magrittr_1.5
methods_3.6.3 mime_0.9 openssl_1.4.2
packrat_0.5.0 promises_1.1.1 R6_2.4.1
Rcpp_1.0.5 rlang_0.4.7 rsconnect_0.8.16
rstudioapi_0.11 shiny_1.5.0 shinyWidgets_0.5.3
sourcetools_0.1.7 stats_3.6.3 sys_3.4
tools_3.6.3 utils_3.6.3 withr_2.2.0
xfun_0.16 xtable_1.8-4 yaml_2.2.1
By filing an issue to this repo, I promise that
- [x] I have fully read the issue guide at https://yihui.name/issue/.
- [x] I have provided the necessary information about my issue.
- If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
- If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included
xfun::session_info('DT')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('rstudio/DT')
. - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [x] I have learned the Github Markdown syntax, and formatted my issue correctly.
I understand that my issue may be closed if I don't fulfill my promises.
It looks like a duplicate of #517 . The cause of this issue is that the extra JS/CSS library can't be imported properly in such cases. In my opinion, we may only able to fix this via a global option as commented in #517 .
Thanks for the comment. As I said, it's relatively easy to avoid it. I just wanted to make sure others know, even though I evidently wasn't the first to report it :)