显示validate()
函数的工作example app from the shiny docs:
## server.R
`%then%` <- shiny:::`%OR%`
shinyServer(function(input, output) {
data <- reactive({
validate(
need(input$data != "", "Please select a data set") %then%
need(input$data %in% c("mtcars", "faithful", "iris"),
"Unrecognized data set") %then%
need(input$data, "Input is an empty string") %then%
need(!is.null(input$data),
"Input is not an empty string, it is NULL")
)
get(input$data, 'package:datasets')
})
output$plot <- renderPlot({
hist(data()[, 1], col = 'forestgreen', border = 'white')
})
output$table <- renderTable({
head(data())
})
})
## ui.R
shinyUI(fluidPage(
tags$head(
tags$style(HTML("
.shiny-output-error-validation {
color: green;
}
"))
),
titlePanel("Validation App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set",
choices = c("", "mtcars", "faithful", "iris"))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("plot"),
tableOutput("table")
)
)
))
有没有办法让应用只显示一次validate
错误消息而不是两次,因为相同的验证对plot
失败一次,对{失败一次失败{1}}元素?
答案 0 :(得分:3)
我想应该有更好的方法来解决这个问题,但这是我唯一的想法:
server.R
## server.R
`%then%` <- shiny:::`%OR%`
shinyServer(function(input, output) {
values <- reactiveValues(test=data.frame())
data <- reactive({
validate(
need(input$data != "", "Please select a data set") %then%
need(input$data %in% c("mtcars", "faithful", "iris"),
"Unrecognized data set") %then%
need(input$data, "Input is an empty string") %then%
need(!is.null(input$data),
"Input is not an empty string, it is NULL")
)
get(input$data, 'package:datasets')
})
observeEvent(data(), {
if(!is.null(data())) {
values$test <- data()
} else {
values$test <- NULL
}
})
output$plot <- renderPlot({
hist(data()[, 1], col = 'forestgreen', border = 'white')
})
output$table <- renderTable({
head(values$test)
})
})