View source: R/spsServerCollections.R
spsValidate | R Documentation |
this function is used on server side to usually validate input dataframe or some expression. The usage is similar to shiny::validate but is not limited to shiny render functions and provides better user notification and server-end logging (dual-end logging).
spsValidate(
expr,
vd_name = "my validation",
pass_msg = glue("validation: '{vd_name}' passed"),
shiny = TRUE,
verbose = spsOption("verbose"),
prefix = ""
)
expr |
the expression to validate data or other things. Use
If the expression fails, it will block the code following this function within
the same reactive domain to continue, similar to |
vd_name |
validate title |
pass_msg |
string, if pass, what message do you want to show |
shiny |
bool, show message on console but hide from users?
see |
verbose |
bool, show pass message? Default follows global verbose
setting, use spsUtil::spsOption to set up the value |
prefix |
see |
Since spsComps 0.3.1 to have the message displayed on shiny UI, you don't need
to attach the dependencies manually by adding spsDepend("spsValidate")
or
spsDepend("toastr")
(old name) on UI. This becomes optional, only in the case
that automatic attachment is not working.
If expression fails, block the code following this validation function
and no final return, else TRUE
.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.