Skip to contents

Let user upload a file and import data

Usage

import_file_ui(
  id,
  title = TRUE,
  preview_data = TRUE,
  file_extensions = c(".csv", ".txt", ".xls", ".xlsx", ".rds", ".fst", ".sas7bdat",
    ".sav"),
  layout_params = c("dropdown", "inline")
)

import_file_server(
  id,
  btn_show_data = TRUE,
  show_data_in = c("popup", "modal"),
  trigger_return = c("button", "change"),
  return_class = c("data.frame", "data.table", "tbl_df", "raw"),
  reset = reactive(NULL),
  read_fns = list()
)

Arguments

id

Module's ID.

title

Module's title, if TRUE use the default title, use NULL for no title or a shiny.tag for a custom one.

preview_data

Show or not a preview of the data under the file input.

file_extensions

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

layout_params

How to display import parameters : in a dropdown button or inline below file input.

btn_show_data

Display or not a button to display data in a modal window if import is successful.

show_data_in

Where to display data: in a "popup" or in a "modal" window.

trigger_return

When to update selected data: "button" (when user click on button) or "change" (each time user select a dataset in the list).

return_class

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

reset

A reactive function that when triggered resets the data.

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

    • na.strings: character(s) to interpret as missing values.

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(
  # theme = bslib::bs_theme(version = 5L),
  # theme = bslib::bs_theme(version = 5L, preset = "bootstrap"),
  tags$h3("Import data from a file"),
  fluidRow(
    column(
      width = 4,
      import_file_ui(
        id = "myid",
        file_extensions = c(".csv", ".txt", ".xls", ".xlsx", ".json"),
        layout_params = "inline" # or "dropdown"
      )
    ),
    column(
      width = 8,
      tags$b("Import status:"),
      verbatimTextOutput(outputId = "status"),
      tags$b("Name:"),
      verbatimTextOutput(outputId = "name"),
      tags$b("Code:"),
      verbatimTextOutput(outputId = "code"),
      tags$b("Data:"),
      verbatimTextOutput(outputId = "data")
    )
  )
)
#> Warning: i18n: translation for 'Missing values character(s):' not found!
#> Warning: i18n: translation for 'if several use a comma (',') to separate them' not found!

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

  imported <- import_file_server(
    id = "myid",
    # Custom functions to read data
    read_fns = list(
      xls = function(file, sheet, skip, encoding) {
        readxl::read_xls(path = file, sheet = sheet, skip = skip)
      },
      json = function(file) {
        jsonlite::read_json(file, simplifyVector = TRUE)
      }
    ),
    show_data_in = "modal"
  )

  output$status <- renderPrint({
    imported$status()
  })
  output$name <- renderPrint({
    imported$name()
  })
  output$code <- renderPrint({
    imported$code()
  })
  output$data <- renderPrint({
    imported$data()
  })

}

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