R/connections.R

Defines functions .get_folder get_remote_directory

Documented in get_remote_directory

#' Create a Path to the People Survey Shared-Drive Area
#'
#'#' @description
#' Get the remote directory on the network drive that contains the People
#' Survey data.
#'
#' @param dir Character. A pre-supplied path to the remote directory. Defaults
#'     to `NULL`.
#'
#' @details
#' If a macOS user is connected to the network drive and the path exists the
#' function returns without user action. On Windows or on a Mac with a different
#' mount point, the user will be presented with an RStudio GUI dialog to select
#' the directory. This must be the "People Survey" folder, not anywhere deeper
#' in the file tree. Assumes the user is using the RStudio IDE.
#'
#' @return A charcter string.
#'
#' @export
#'
#' @examples \dontrun{get_remote_directory()}
get_remote_directory <- function(dir = NULL) {

  if (!is.null(dir)) {
    if (dir.exists(dir)) {
      x <- dir
    } else {
      stop("Path does not exist")
    }
  } else {
    if (.Platform$OS.type == "unix") {
      dir <- c("/Volumes/SCSPS/People Survey", "/Volumes/People Survey")
      if (sum(dir.exists(dir) > 0)) {
        x <- dir[dir.exists(dir)]
      } else {
        x <- .get_folder()
      }
    } else {
      x <- .get_folder()
    }
  }

  # Verify drive connection against checksum file
  y <- file.path(x, ".csps_drive_verify")
  stop_me <- TRUE
  if (file.exists(y)) {
    # check contents of file is expected
    # rlang::hash("Civil Service People Survey")
    y <- scan(y, what = character(), quiet = TRUE)
    if (y == "1dca05f0aa5eecfd75fc5951577feb15") {
      stop_me <- FALSE
      message("Connection to shared drive verified @ ", x)
    }
  }
  if (stop_me) {
    rstudioapi::showDialog(
      title = "Invalid directory",
      message = "You have not selected the People Survey folder"
    )
    stop("You have not selected the People Survey folder")
  }

  return(x)

}

# Internal: open dialog box and prompt user to interactively select the People
# Survey folder from the network drive.
.get_folder <- function() {
  rstudioapi::selectDirectory(
    caption = "Select People Survey folder",
    label = "Must be People Survey"
  )
}
co-analysis/cspstools documentation built on Sept. 20, 2022, 1:10 a.m.