diff --git a/R/read_sharepoint.R b/R/read_sharepoint.R index ca3f9b8..c8d517b 100644 --- a/R/read_sharepoint.R +++ b/R/read_sharepoint.R @@ -3,7 +3,8 @@ #' #' @description #' [read_sharepoint()] is designed to download a SharePoint item to a temporary -#' folder and read the file based on the file extension. The class of the +#' folder and read the file based on the file extension. If a function is +#' provided to .f, it is used to read the downloaded file. If not, class of the #' returned object depends on the file extension of the file parameter. #' #' - If the file is a csv, csv2, or tsv file or a Microsoft Excel file (xlsx or @@ -24,14 +25,16 @@ #' @param file Required. A SharePoint shared file URL, document URL, or, if #' `item_id` is supplied, a file name to use in combination with `new_path` to #' set `dest` with location and filename for downloaded item. +#' @param .f Optional function to use to read file downloaded from SharePoint. #' @param ... Additional parameters passed to one of the functions identified in -#' the description. +#' the description or supplied to `.f` #' @inheritParams download_sp_item #' @inheritParams get_sp_drive #' @inheritParams get_sp_site #' @export read_sharepoint <- function(file, ..., + .f = NULL, new_path = tempdir(), overwrite = TRUE, drive_name = NULL, @@ -56,33 +59,49 @@ read_sharepoint <- function(file, message <- "Reading item with " + if (!is.null(.f)) { + .f <- as_function(.f) + cli_progress_step("{message}{.fn {as_name(expr(.f))}}") + return(.f(dest, ...)) + } + if (is_fileext_path(dest, c("csv", "csv2", "tsv"))) { check_installed("readr") cli_progress_step("{message}{.fn readr::read_delim}") - readr::read_delim(dest, ...) - } else if (is_fileext_path(dest, c("xlsx", "xls"))) { + return(readr::read_delim(dest, ...)) + } + + if (is_fileext_path(dest, c("xlsx", "xls"))) { check_installed("readxl") cli_progress_step("{message}{.fn readxl::read_excel}") - readxl::read_excel(dest, ...) - } else if (is_fileext_path(dest, "rds")) { + return(readxl::read_excel(dest, ...)) + } + + if (is_fileext_path(dest, "rds")) { check_installed("readr") cli_progress_step("{message}{.fn readr::read_rds}") - readr::read_rds(dest, ...) - } else if (is_fileext_path(dest, "docx")) { + return(readr::read_rds(dest, ...)) + } + + if (is_fileext_path(dest, "docx")) { check_installed("officer") cli_progress_step("{message}{.fn officer::read_docx}") - officer::read_docx(dest) - } else if (is_fileext_path(dest, "pptx")) { + return(officer::read_docx(dest)) + } + + if (is_fileext_path(dest, "pptx")) { check_installed("officer") cli_progress_step("{message}{.fn officer::read_pptx}") officer::read_pptx(dest) - } else if (is_fileext_path(dest, c("gpkg", "geojson", "kml", "gdb", "shp"))) { + } + + if (is_fileext_path(dest, c("gpkg", "geojson", "kml", "gdb", "shp"))) { check_installed("sf") cli_progress_step("{message}{.fn sf::read_sf}") - sf::read_sf(dest, ...) - } else { - check_installed("readr") - cli_progress_step("{message}{.fn readr::read_lines}") - readr::read_lines(dest, ...) + return(sf::read_sf(dest, ...)) } + + check_installed("readr") + cli_progress_step("{message}{.fn readr::read_lines}") + readr::read_lines(dest, ...) } diff --git a/R/write_sharepoint.R b/R/write_sharepoint.R index 527fec9..df9b506 100644 --- a/R/write_sharepoint.R +++ b/R/write_sharepoint.R @@ -17,6 +17,8 @@ #' @param file File to write to. Passed to `file` parameter for #' [readr::write_csv()] or [readr::write_rds()], `dsn` for `sf::write_sf()`, #' or `target` for `print()` (when working with `{officer}` class objects). +#' @param .f Optional function to write the input data to disk before uploading +#' file to SharePoint. #' @param ... Additional parameters passed to write function. #' @param new_path Path to write file to. Defaults to [tempdir()] #' @param blocksize Additional parameter passed to `upload_folder` or @@ -30,6 +32,7 @@ write_sharepoint <- function(x, file, dest, ..., + .f = NULL, new_path = tempdir(), overwrite = FALSE, drive_name = NULL, @@ -44,7 +47,10 @@ write_sharepoint <- function(x, check_string(file, call = call) file <- str_c_fsep(new_path, file) - if (inherits(x, "sf")) { + if (!is.null(.f)) { + .f <- as_function(.f) + .f(x, file, ...) + } else if (inherits(x, "sf")) { check_installed("sf", call = call) sf::write_sf(x, dsn = file, ...) } else if (inherits(x, "data.frame") && !stringr::str_detect(file, ".xlsx$")) {