import-modal: Import from all sources

import-modalR Documentation

Import from all sources

Description

Wrap all import modules into one, can be displayed inline or in a modal window..

Usage

import_ui(
  id,
  from = c("env", "file", "copypaste", "googlesheets", "url"),
  file_extensions = c(".csv", ".txt", ".xls", ".xlsx", ".rds", ".fst", ".sas7bdat",
    ".sav")
)

import_server(
  id,
  validation_opts = NULL,
  allowed_status = c("OK", "Failed", "Error"),
  return_class = c("data.frame", "data.table", "tbl_df", "raw"),
  read_fns = list()
)

import_modal(
  id,
  from,
  title = "Import data",
  size = "l",
  file_extensions = c(".csv", ".txt", ".xls", ".xlsx", ".rds", ".fst", ".sas7bdat",
    ".sav")
)

Arguments

id

Module's id

from

The import_ui & server to use, i.e. the method. There are 5 options to choose from. ("env", "file", "copypaste", "googlesheets", "url")

file_extensions

File extensions accepted by shiny::fileInput(), can also be MIME type.

validation_opts

list of arguments passed to [validation_server().

allowed_status

Vector of statuses allowed to confirm dataset imported, if you want that all validation rules are successful before importing data use allowed_status = "OK".

return_class

Class of returned data: data.frame, data.table, tbl_df (tibble) or raw.

read_fns

Named list with custom function(s) to read data:

  • the name must be the extension of the files to which the function will be applied

  • the value must be a function that can have 5 arguments (you can ignore some of them, but you have to use the same names), passed by user through the interface:

    • file: path to the file

    • sheet: for Excel files, sheet to read

    • skip: number of row to skip

    • dec: decimal separator

    • encoding: file encoding

title

Modal window title.

size

Modal window size, default to "l" (large).

Value

  • UI: HTML tags that can be included in shiny's UI

  • Server: a list with three slots:

    • status: a reactive function returning the status: NULL, error or success.

    • name: a reactive function returning the name of the imported data as character.

    • data: a reactive function returning the imported data.frame.

Examples


library(shiny)
library(datamods)

ui <- fluidPage(
  # Try with different Bootstrap version
  # theme = bslib::bs_theme(version = 4),
  fluidRow(
    column(
      width = 4,
      checkboxGroupInput(
        inputId = "from",
        label = "From",
        choices = c("env", "file", "copypaste", "googlesheets", "url"),
        selected = c("file", "copypaste")
      ),
      actionButton("launch_modal", "Launch modal window")
    ),
    column(
      width = 8,
      tags$b("Imported data:"),
      verbatimTextOutput(outputId = "name"),
      verbatimTextOutput(outputId = "data")
    )
  )
)

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

  observeEvent(input$launch_modal, {
    req(input$from)
    import_modal(
      id = "myid",
      from = input$from,
      title = "Import data to be used in application"
    )
  })

  imported <- import_server("myid", return_class = "tbl_df")

  output$name <- renderPrint({
    req(imported$name())
    imported$name()
  })

  output$data <- renderPrint({
    req(imported$data())
    imported$data()
  })
}

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

datamods documentation built on Sept. 26, 2023, 5:07 p.m.