validation: Validation module

Description Usage Arguments Value Examples

Description

Check that a dataset respect some validation expectations.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
validation_ui(id, display = c("dropdown", "inline"), max_height = NULL, ...)

validation_server(
  id,
  data,
  n_row = NULL,
  n_col = NULL,
  n_row_label = "Valid number of rows",
  n_col_label = "Valid number of columns",
  btn_label = "Dataset validation:",
  rules = NULL
)

Arguments

id

Module's ID.

display

Display validation results in a dropdown menu by clicking on a button or display results directly in interface.

max_height

Maximum height for validation results element, useful if you have many rules.

...

Arguments passed to actionButton or uiOutput depending on display mode, you cannot use inputId/outputId, label or icon (button only).

data

a reactive function returning a data.frame.

n_row, n_col

A one-sided formula to check number of rows and columns respectively, see below for examples.

n_row_label, n_col_label

Text to be displayed with the result of the check for number of rows/columns.

btn_label

Label for the dropdown button, will be followed by validation result.

rules

An object of class validator created with validate::validator.

Value

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
library(datamods)
library(shiny)
library(validate)

# Define some rules to be applied to data
myrules <- validator(
  is.character(Manufacturer) | is.factor(Manufacturer),
  is.numeric(Price),
  Price > 12, # we should use 0 for testing positivity, but that's for the example
  !is.na(Luggage.room),
  in_range(Cylinders, min = 4, max = 8),
  Man.trans.avail %in% c("Yes", "No")
)
# Add some labels
label(myrules) <- c(
  "Variable Manufacturer must be character",
  "Variable Price must be numeric",
  "Variable Price must be strictly positive",
  "Luggage.room must not contain any missing values",
  "Cylinders must be between 4 and 8",
  "Man.trans.avail must be 'Yes' or 'No'"
)
# you can also add a description()

ui <- fluidPage(
  tags$h2("Validation"),
  fluidRow(
    column(
      width = 4,
      radioButtons(
        inputId = "dataset",
        label = "Choose dataset:",
        choices = c("mtcars", "MASS::Cars93")
      ),
      tags$p("Dropdown example:"),
      validation_ui("validation1"),

      tags$br(),

      tags$p("Inline example:"),
      validation_ui("validation2", display = "inline")
    ),
    column(
      width = 8,
      tags$b("Status:"),
      verbatimTextOutput("status"),
      tags$b("Details:"),
      verbatimTextOutput("details")
    )
  )
)

server <- function(input, output, session) {

  dataset <- reactive({
    if (input$dataset == "mtcars") {
      mtcars
    } else {
      MASS::Cars93
    }
  })

  results <- validation_server(
    id = "validation1",
    data = dataset,
    n_row = ~ . > 20, # more than 20 rows
    n_col = ~ . >= 3, # at least 3 columns
    rules = myrules
  )

  validation_server(
    id = "validation2",
    data = dataset,
    n_row = ~ . > 20, # more than 20 rows
    n_col = ~ . >= 3, # at least 3 columns
    rules = myrules
  )

  output$status <- renderPrint(results$status())
  output$details <- renderPrint(results$details())

}

if (interactive())
  shinyApp(ui, server)

datamods documentation built on July 2, 2021, 5:07 p.m.