I'm getting a "datatables warning table id=datatables_table_0 shiny error" randomly and sporadically in my Shiny application that's hosted on shinyapp.io. I can't seem to replicate but have users encountering the issue. Other related posts say to explicitly use the DT package which I am doing. I'm aware the code is long but since I can't reproduce the error I'm not sure what's okay to leave out.
Code Module that's experiencing the issue:
endocrine_igf_mod_ui <- function(id) {
ns <- NS(id)
tagList(
fluidPage(
DT::dataTableOutput(ns("igfTable")))
)
}
endocrine_igf_mod <-
function(input,output,session,patient_id,
labs, visit) {
labs_visit <- reactive({left_join(labs(), visit(), by= c("subj_id", "visit_num")) })
igfData <- reactive({
labs_visit() %>% select(-unit) %>%
filter(subj_id == patient_id()) %>%
filter(test %in% c("Cort Serum", "Cortisol",
"Cortisol AM", "Cortisol T","Cortisol T.", "Cortisol, T",
"IGF-1","IGF-1 (BL)", "somatomedin","Somatomedin", "PTH",
"Calcium T.","Calcium Tot",
"Calcium, To","Calcium,T" ,"Calcium,tot", "Calcium",
"Cal Ion","Cal, Ion.",
"Calc. Ion,", "Calcium Ion","Calcium Ionized",
"Calcium, ionized","Calcium, Ionized","CALCIUM, IONIZED",
"ionized cal", "Ionized cal",
"Calium Ionized"
)) %>%
mutate(test = ifelse(test %in% c("IGF-1","IGF-1 (BL)","somatomedin","Somatomedin"),
"IGF-1",
ifelse(test %in% c("Cal Tot", "Calcium",
"Calcium T.","Calcium Tot",
"Calcium, To","Calcium,T" ,"Calcium,tot"),
"Calcium Total",
ifelse(test %in% c("Cal Ion","Cal, Ion.",
"Calc. Ion,",
"Calcium Ion","Calcium Ionized","Calcium, ionized",
"Calcium, Ionized","CALCIUM, IONIZED",
"Calium Ionized", "ionized cal", "Ionized cal" ),
"Ionized Calcium",
ifelse(test %in% c("Cort Serum", "Cortisol",
"Cortisol AM", "Cortisol T","Cortisol T.",
"Cortisol, T"),
"Cortisol",
test))))) %>%
mutate(test = factor(test, levels=c("Cortisol", "IGF-1","PTH","Calcium Total","Ionized Calcium"))) %>% #reorder
filter(!is.na(result)) %>%
filter(!is.na(visit_date)) %>%
arrange(desc(visit_date)) %>%
distinct(visit_date, test, source, .keep_all = TRUE) %>%
spread(test, result) %>%
select(visit_date, everything(),-visit_num, - id, -subj_id) %>%
rename(`Visit Date` = visit_date, Source=source) %>%
as.data.frame()
})
output$igfTable <- DT::renderDataTable({
req(patient_id()!="")
IGF_1_formatter <- formatter("span",
style = x ~ style(
display = "block",
"font.weight" = "bold",
"color" = "white",
"border-radius" = "4px",
"padding-right" = "4px",
font.weight = "bold",
ifelse(is.na(x), "background-color:transparent",
ifelse(x < 100 , "background-color:gold",
ifelse(x>= 100 & x<= 279,
"background-color:seagreen",
"background-color:red")))))
cortisol_formatter <- formatter("span",
style = x ~ style(
display = "block",
"font.weight" = "bold",
"color" = "white",
"border-radius" = "4px",
"padding-right" = "4px",
font.weight = "bold",
ifelse(is.na(x), "background-color:transparent",
ifelse(x >= 4.0 & x <= 22.0,
"background-color:seagreen",
"background-color:red"))))
formattable(igfData() %>% arrange(desc(`Visit Date`)),
table.attr = 'style=" background-color: white; font-size: 22px";',
list(
`IGF-1` = IGF_1_formatter,
Cortisol = cortisol_formatter
)) %>%
as.datatable(
options = list(
columnDefs = list(list(className = 'dt-center', targets = '_all')),
dom = "t",
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#95959C', 'color': '#fff'});",
"}"),
pageLength = -1
),
class = "cell-border stripe",
selection = "none",
rownames = FALSE) %>%
DT::formatStyle(columns = colnames(igfData()), fontSize = '22px', fontWeight = 'bold')
}
)
}