R/path_check.R

Defines functions path_check

Documented in path_check

#' @title Check a path
#' @description Accessory functions to check that a directory exists and
#'  the user have write permissions on it (to be used in a Shiny context)
#' @param path `string` full path to a folder
#' @param mustbe_empty `logical` if TRUE, accept only empty directories
#' @param mustbe_writable `logical` if TRUE, accept only directories with write 
#'  permissions
#' @rdname path_check
#' @author Luigi Ranghetti, phD (2019)
#' @author Lorenzo Busetto, PhD (2019)
#' @keywords internal
path_check <- function(path, mustbe_empty = FALSE, mustbe_writable = TRUE) {
  
  if (all(length(path)>0, path[1]!="")) {
    if (!dir.exists(path)) {
      outtext <- "\u2718 (the directory does not exist)"
      isvalid <- FALSE
    } else if (mustbe_empty==TRUE & length(list.files(path, all.files = TRUE))>2) {
      outtext <- "\u2718 (the directory is not empty)"
      isvalid <- FALSE
    } else if (mustbe_writable == TRUE & file.access(path, mode=2)<0) {
      outtext <- "\u2718 (missing write permissions)"
      isvalid <- FALSE
    } else {
      outtext <- "\u2714"
      isvalid <- TRUE
    }
    #
  } else {
    outtext <- ""
    isvalid <- NA
  }

  out <- if (requireNamespace("shiny", quietly = TRUE)) {
    if (is.na(isvalid)) {
      shiny::renderText(outtext)
    } else if (isvalid) {
      shiny::renderUI(shiny::span(style="color:darkgreen", outtext))
    } else {
      shiny::renderUI(shiny::span(style="color:red", outtext))
    }
  } else {
    outtext
  }
  attr(out, "isvalid") <- isvalid
  return(out)
  
}

Try the sen2r package in your browser

Any scripts or data that you put into this service are public.

sen2r documentation built on Nov. 10, 2023, 9:08 a.m.