Skip to content
Advertisement

Need to replace default “No data available in table” message in Shiny R renderDataTable

I don’t know much of JavaScript, and I’m having a problem to override the default message in the Shiny R App, when I’m outputting table.

When table is empty, it’s giving message "No data available in table" in the first row. I wanted to put some app-specific instructions instead.

I found that there’s something like: options = list(searching = FALSE,paging = FALSE) but don’t know what option it would be to switch that text.

Also, I found JS code to reset the message (https://datatables.net/reference/option/language.zeroRecords), but I wasn’t able to attach this correctly to renderDataTable in Shiny. I just don’t know the correct syntax of incorporating JS into shiny, I tried

options = list(searching = FALSE,paging = FALSE, callback=DT:JS(
'
{
  "language": {
    "zeroRecords": "No records to display- custom text"
  }
'

but it didn’t work. I would appreciate some guidance on this. Here’s the whole code. Right now my attempts to replace the mesage are ignored:

library(ggplot2)
library(DT)
ui <- fluidPage(
titlePanel("Basic DataTable"),

# Create a new Row in the UI for selectInputs
fluidRow(
column(12,
       selectInput("man",
                   "Manufacturer:",
                   c("All",
                     unique(as.character(mpg$manufacturer))))
 )
),
# Create a new row for the table.
fluidRow(
DT::dataTableOutput("table")
)
)
server <-function(input, output) {

# Filter data based on selections
output$table <- DT::renderDataTable(DT::datatable({
data <- mpg
if (input$man != "All") {
  data <- data[data$manufacturer == "dddddd",]
}
data
},options = list(searching = FALSE,paging = FALSE,callback=DT::JS(
'
{
"language": {
"zeroRecords": "No records to display- custom text"
}}
') )
))
 }
 shinyApp(ui = ui, server = server)

Advertisement

Answer

Do not use the callback, you can directly set the language -> zeroRecords attribute using the options parameter:

server <- function(input, output) {
  # Filter data based on selections
  output$table <- DT::renderDataTable(DT::datatable({
    data <- mpg
    if (input$man != "All") {
      data <- data[data$manufacturer == "dddddd",]
    }
    data
  }, options = 
    list(searching = FALSE,paging = FALSE,
         language = list(
           zeroRecords = "No records to display - custom text")              
  )))
}

This works for me.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement