R 中 DT::renderDataTable 的空表的错误消息

Ans*_*shu 1 r shiny

我需要在 UI 屏幕上显示来自表格的数据。我目前正在使用 DT::renderDataTable。

在 renderDataTable 内部,代码将返回从表返回的数据。如果表中没有可用数据,屏幕上将显示空白。

当表中没有数据时,我需要添加一些客户错误消息,你能帮忙吗?

Joh*_*ene 5

有不同的方法。

显示通知

library(DT)
library(shiny)

ui <- fluidPage(
    actionButton("load", "Load/unload data"),
    DTOutput("table")
)

server <- function(input, output, session) {
    df <- eventReactive(input$load, {
        if(input$load %% 2 == 0){
            return(cars)
        } else {
            shiny::showNotification("No data", type = "error")
            NULL
        }
    })

    output$table <- renderDT(df())
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

显示错误

library(DT)
library(shiny)

ui <- fluidPage(
    actionButton("load", "Load/unload data"),
    DTOutput("table")
)

server <- function(input, output, session) {
    df <- reactive({
        if(input$load %% 2 == 0){
            dat <- cars
        } else {
            dat <- NULL
        }

        validate(
            need(!is.null(dat), "No data")
        )

        return(dat)
    })

    output$table <- renderDT(df())
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

您还可以使用 来显示模态showModal