R/mod_get_layers.R

Defines functions mod_get_layers_Server mod_get_layers_UI

Documented in mod_get_layers_Server mod_get_layers_UI

#' Upload GeoPackages to a Shiny App
#'
#' @description Generate UI component for file upload using Shiny's \code{fileInput} to allow users to upload GeoPackages to a Shiny app.
#'
#' @param id inputID for file upload UI component.
#' @param label label for file upload widget on the UI.
#' @param multiple logical, whether to accept multiple file uploads.
#' @param accept file types to accept - vector of strings indicating extensions or MIME type
#'
#' @import shiny
#' @export
#'
mod_get_layers_UI <- function(id, label, multiple, accept) {
  tagList(
    # Input: Select a file ----
    fileInput(NS(id, "get_layers"),
      label,
      multiple = multiple,
      accept = accept
    ),
  )
}

#' Process user uploaded GeoPackages within a Shiny app
#'
#' @description Process files (GeoPackages) uploaded by the user. For each file
#'   uploaded, call \code{list_layers} to list all layers in the GeoPacakge
#'   and the temporary location of the GeoPackage.
#' @param id inputID to correspond to file upload UI component.
#'
#' @return \code{field_layers} tibble with three columns: layers - layer name;
#'   layer_disp_name - clean and informative layer name for select
#'   input; file_path - temporary file path to data; and file_type - file type.
#'
#' @import shiny
#' @export

mod_get_layers_Server <- function(id) {
  moduleServer(id, function(input, output, session) {
    field_layers <- reactive({
      req(input$get_layers$datapath)

      n_file <- input$get_layers$datapath
      n_name <- input$get_layers$name

      f_lyrs <- tryCatch(
        error = function(cnd) NULL,
        purrr::map2(n_file, n_name, list_layers) %>%
          dplyr::bind_rows()
      )
    })
    return(field_layers)
  })
}
livelihoods-and-landscapes/maplandscape documentation built on March 20, 2023, 5:43 a.m.