dreamRs / datamods

Shiny modules to import and manipulate data into an application or addin
https://dreamrs.github.io/datamods/
GNU General Public License v3.0
138 stars 35 forks source link

Example/documentation request #31

Closed kyleweise closed 3 years ago

kyleweise commented 3 years ago

Hi! Interested in using two of the modules here, the file import module , and the validation module . I was wondering if someone could provide me with a minimal example of how to use the two together. Namely, let user upload a data file and then run some validation against that data.

This would be really helpful! Thanks, and great package/modules!

-Kyle

pvictor commented 3 years ago

Hello,

The easiest way to use both together is to use import_ui or import_modal modules because it already combine the two. But if you want to something more custom, here's an example :

library(shiny)
library(shinyWidgets)
library(datamods)
library(validate)

# Validation rules --------------------------------------------------------

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'"
)

# App ---------------------------------------------------------------------

ui <- fluidPage(
  fluidRow(
    column(
      width = 4,
      tags$h3("Import File :"),
      # import_file_ui("import", title = NULL)
      import_globalenv_ui("import", title = NULL)
    ),
    column(
      width = 4,
      tags$h3("Validate Data :"),
      validation_ui("validate", display = "inline")
    ),
    column(
      width = 4,
      uiOutput("action"),
      verbatimTextOutput("validation_status"),
      verbatimTextOutput("data")
    )
  )
)

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

  # imported <- import_file_server("import", trigger_return = "change")
  imported <- import_globalenv_server("import", trigger_return = "change")
  validated <- validation_server(
    id = "validate",
    data = imported$data,
    n_row = ~ . > 20, # more than 20 rows
    n_col = ~ . >= 3, # at least 3 columns
    rules = myrules
  )

  output$action <- renderUI({
    status <- validated$status()
    if (is.null(status)) {
      alert("You need to import a file", status = "info")
    } else if (identical(status, "Error")) {
      alert("Error ! Not a good file", status = "danger")
    } else {
      alert("Yes! This file is good", status = "success")
    }
  })

  output$validation_status <- renderPrint({
    validated$status()
  })
  output$data <- renderPrint({
    imported$data()
  })

}

shinyApp(ui, server)

Let me know if it helps you

Victor

kyleweise commented 3 years ago

@pvictor this is exactly what I was looking for! Thanks. Appreciate the fast response.