3  Sharepoint

Sharepoint is part of many data pipelines, and the easiest way to interact with Sharepoint from R is through the {Microsoft365R} package. This package has been vetted by the Department of Technology (DT) and authentication should be straight-forward after calling one of the initial functions.

library(Microsoft365R)

wp <- get_sharepoint_site("Workforce Programs")
wp_docs <- wp$get_drive("Documents")

wp_docs$list_files()

wp_docs$download_file("path_to_file.xlsx")

It is helpful to wrap common read/write operations into more usable functions:

connect_to_sharepoint_site_docs <- function(sp_site) {
  sp_site <- Microsoft365R::get_sharepoint_site(sp_site)
  sp_site_doc <- sp_site$get_drive("Documents")
  return(sp_site_doc)
}

connect_to_wp_docs <- function() connect_to_sharepoint_site_docs("Workforce Programs")

download_from_wp <- function(sp_file, destination) {
  docs <- connect_to_wp_docs()
  docs$download_file(sp_file, dest = destination)
  cli::cli_alert_success(glue::glue("{sp_file} downloaded."))
}

upload_to_wp <- function(file, sp_destination) {
  docs <- connect_to_wp_docs()
  docs$upload_file(
    file,
    sp_location
  )
  cli::cli_alert_success(glue::glue("{file} uploaded."))
}

read_wp <- function(path) {
  tmp <- tempfile(fileext = "xlsx")
  download_from_wp(
    sp_file = glue("{path}.xlsx"),
    destination = tmp
  )
  out <- readxl::read_excel(tmp, .name_repair = janitor::make_clean_names)
  return(out)
}