R/fct_read_tables.R

Defines functions read_tables

Documented in read_tables

#' Read tabular data - either spatial layer (i.e. a table in a GeoPackage) or csv file.
#'
#' @param uploads Data frame generated from \code{list_layers} indicating names, file types, and paths to uploaded layers.
#' @param lyr User selected layer - this is used for selecting the relevant layer from a GeoPackage with many layers.
#'
#' @return  a (spatial) data frame of the selected layer.
#'
#' @import shiny
#' @export

read_tables <- function(uploads, lyr) {
  req(uploads, lyr)
  a_lyr <- uploads %>%
    dplyr::filter(uploads[["layer_disp_name_idx"]] == lyr)

  if (a_lyr$file_type == "gpkg") {
    a_df <- sf::st_read(a_lyr$file_path, layer = a_lyr$layers, stringsAsFactors = FALSE)
    if ("sf" %in% class(a_df)) {
      a_df <- sf::st_sf(sf::st_set_geometry(a_df, NULL), geometry = sf::st_geometry(a_df))
    }
  } else if (a_lyr$file_type == "csv") {
    a_df <- readr::read_csv(a_lyr$file_path)
  }
  a_df
}
livelihoods-and-landscapes/maplandscape documentation built on March 20, 2023, 5:43 a.m.