我有一个有许多情节的Shiny网络应用程序。每个绘图都有自己的SQL查询来获取数据。其中一个查询可能会返回一个数据不足的表。如果发生这种情况,那么我想在选项卡中显示一条文本消息。
server.R:
library(shiny)
library(RMySQL)
shinyServer(function(input, output, session) {
output$lineGraphOne <- renderPlot({
table <- getDataSomeHowOne()
if(dim(table[1]) < 3) {
error <- paste("Some error message")
} else {
plot(x = as.Date(table$date), y = table$count)
}
})
output$lineGraphTwo <- renderPlot({
table <- getDataSomeHowTwo()
if(dim(table[1]) < 3) {
error <- paste("Some error message")
} else {
plot(x = as.Date(table$date), y = table$count)
}
})
})
ui.R
library(shiny)
shinyUI(navbarPage("Title",
tabPanel("Name",
sidebarLayout(
mainPanel(
tabsetPanel(id = "tabs",
tabPanel("One", plotOutput("lineGraphOne")),
tabPanel("Two", plotOutput("lineGraphTwo"))
)
),
sidebarPanel(
dateInput('queryDate', 'Datum:', value = as.Date("2010-04-09"))
)
)
)
))
如何在相应的标签中显示错误字符串而不是图表?
答案 0 :(得分:4)
请查看validate
,请注意该示例来自Write error messages for your UI with validate
rm(list = ls())
library(shiny)
runApp(list(
ui = (fluidPage(
titlePanel("Validation App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set",
choices = c("", "mtcars", "faithful", "iris"))
),
# Show a plot of the generated distribution
mainPanel(
tableOutput("table"),
plotOutput("plot")
)
)
)),
server = function(input, output) {
data <- reactive({
validate(
need(input$data != "", "Please select a data set")
)
get(input$data, 'package:datasets')
})
output$plot <- renderPlot({
hist(data()[, 1], col = 'forestgreen', border = 'white')
})
output$table <- renderTable({
head(data())
})
}
))