我使用renderTable来显示一些数据。但是,有时数据表为空,在这种情况下,我想打印“无数据可显示”或类似内容。renderTable的默认设置是不显示空数据。可以改变吗?怎么样?
您可以在中使用条件renderUi来呈现消息或“ tableOutput”(您不能直接呈现表)
datas <- data.frame()
shiny::runApp(list(
ui = pageWithSidebar(
headerPanel("Example"),
sidebarPanel(
selectInput("dataset", "Dataset", choices = c("iris", "datas"))
),
mainPanel(
uiOutput("ui")
)
),
server = function(input, output, session) {
datasetInput <- reactive({
switch(input$dataset,
"iris" = iris,
"datas" = datas)
})
output$ui <- renderUI({
if(nrow(datasetInput()) == 0)
return("No data to show")
tableOutput("table")
})
output$table <- renderTable({
head(datasetInput())
})
}
))
Run Code Online (Sandbox Code Playgroud)