loadDF: Load tabular files as tibbles to server

View source: R/spsServerCollections.R

loadDFR Documentation

Load tabular files as tibbles to server

Description

load a file to server end. It's designed to be used with a input file source switch button. It uses vroom::vroom to load the file. In SPS, this function is usually combined as downstream of dynamicFileServer() function on on the server side to read the file into R. This loading function only works for parsing tabular data, use vroom::vroom() internally.

If no user data is uploaded, it will return the example dataset that is prepared by the developer. If the developer does not provide the dataset either, it will return a 8-row empty tibble.

Usage

loadDF(
  choice,
  data_init = NULL,
  upload_path = NULL,
  eg_path = NULL,
  comment = "#",
  delim = "\t",
  col_types = vroom::cols(),
  ...
)

Arguments

choice

where this file comes from, one of 'upload' or example 'eg'?

data_init

a tibble to return if upload_path or eg_path is not provided. Return a 8x8 empty tibble if not provided

upload_path

when choice is "upload", where to load the file, will return data_init if this param is not provided

eg_path

when choice is "eg", where to load the file, will return data_init if this param is not provided

comment

comment characters to parse the datafile, see help file of vroom::vroom

delim

delimiter characters to parse the data file, see help file of vroom::vroom

col_types

columns specifications, see help file of vroom::vroom

...

other params for vroom, see help file of vroom::vroom

Details

This is function is wrapped by the shinyCatch() function, so it will show loading information both on console and on UI. This function prevents loading file errors to crash the Shiny app, so any kind of file upload will not crash the app. To show message on UI, spsDepend("toastr") must be used in Shiny UI function, see examples.

Value

returns a tibble and NULL if parsing fails

Examples

if(interactive()){
  # change value to 'local' to see the difference
  spsOption("mode", value = "server")
  ui <- fluidPage(
    spsDepend("toastr"),
    radioButtons(
      "data_source", "Choose your data file source:",
      c("Upload" = "upload", "Example" = "eg"),
      selected = "eg"
    ),
    dynamicFile("data_path", label = "input file"),
    dataTableOutput("df")
  )

  server <- function(input, output, session) {
    tmp_file <- tempfile(fileext = ".csv")
    write.csv(iris, file = tmp_file)
    upload_path <- dynamicFileServer(input, session, "data_path")
    data_df <- reactive({
      loadDF(choice = input$data_source,
             upload_path = upload_path()$datapath,
             delim = ",", eg_path = tmp_file)
    })
    output$df <- renderDataTable(data_df())
  }
  shinyApp(ui, server)
}

systemPipeR/systemPipeShiny documentation built on Oct. 17, 2023, 3:40 a.m.