if(interactive()){
ui <- fluidPage(
spsDepend("spsValidate"), # optional
column(
4,
h3("click below to make the plot"),
p("this button will succeed, verbose on"),
actionButton("vd1", "make plot 1"),
plotOutput("p1")
),
column(
4,
h3("click below to make the plot"),
p("this button will succeed, verbose off"),
actionButton("vd2", "make plot 2"),
plotOutput("p2")
),
column(
4,
h3("click below to make the plot"),
p("this button will fail, no plot will be made"),
actionButton("vd3", "make plot 3"),
plotOutput("p3")
),
column(
4,
h3("click below to make the plot"),
p("this button will fail, but the message is hidden from users"),
actionButton("vd4", "make plot 4"),
plotOutput("p4")
)
)
server <- function(input, output, session) {
mydata <- datasets::iris
observeEvent(input$vd1, {
spsOption("verbose", TRUE) # use global sps verbose setting
spsValidate({
is.data.frame(mydata)
}, vd_name = "Is dataframe")
output$p1 <- renderPlot(plot(iris$Sepal.Length, iris$Sepal.Width))
})
observeEvent(input$vd2, {
spsValidate({
is.data.frame(mydata)
},
vd_name = "Is dataframe",
verbose = FALSE) # use in-function verbose setting
output$p2 <- renderPlot(plot(iris$Sepal.Length, iris$Sepal.Width))
})
observeEvent(input$vd3, {
spsValidate({
is.data.frame(mydata)
if(nrow(mydata) <= 200) stop("Input needs more than 200 rows")
})
print("other things blocked")
output$p3 <- renderPlot(plot(iris$Sepal.Length, iris$Sepal.Width))
})
observeEvent(input$vd4, {
spsValidate({
is.data.frame(mydata)
if(nrow(mydata) <= 200) stop("Input needs more than 200 rows")
}, shiny = FALSE)
print("other things blocked")
output$p4 <- renderPlot(plot(iris$Sepal.Length, iris$Sepal.Width))
})
}
shinyApp(ui, server)
}
# outside shiny example
mydata2 <- list(a = 1, b = 2)
spsValidate({(mydata2)}, "Not empty")
try(spsValidate(stopifnot(is.data.frame(mydata2)), "is dataframe?"), silent = TRUE)
Run the code above in your browser using DataLab