R/utils.R

Defines functions check_dir command_exist check_required_program get_os

get_os <- function(){
  sysinf <- Sys.info()
  if (!is.null(sysinf)){
    os <- sysinf['sysname']
    if (os == 'Darwin')
      os <- "osx"
  } else { ## mystery machine
    os <- .Platform$OS.type
    if (grepl("^darwin", R.version$os))
      os <- "osx"
    if (grepl("linux-gnu", R.version$os))
      os <- "linux"
  }
  tolower(os)
}


check_required_program <- function(){
  os <- get_os()
  if(os == "windows"){
    if(!command_exist("GCSDokan")){
      warning("You do not have <GCSDokan> installed!")
      return(FALSE)
    }
  }else if(os == "linux"||os =="osx"){
    if(!command_exist("gcsfuse")){
      warning("You do not have <gcsfuse> installed!")
      return(FALSE)
    }
  }else{
    warning("Unsupported system")
    return(FALSE)
  }
  return(TRUE)
}

command_exist <- function(x){
  if(get_os()== "windows"){
    result <- suppressWarnings(system2("where", args = x, stdout = TRUE))
    is.null(attr(result, "status"))
  }else{
    result <- suppressWarnings(system2("which", args = x, stdout = TRUE))
    length(result) != 0
  }
}

check_dir<-function(path){
  if(!dir.exists(path)){
    if(file.exists(path)){
      stop("The path <", path, "> is a file, not a directory!")
    }
    if(get_os()!="windows"){
      tryCatch({
        dir.create(path, recursive = TRUE)
      },
      warning=function(w) {
        stop(w)
      })
    }else{
      ## if the path is a driver letter, we do not need to create it
      if(length(grep("[A-Za-z]:/$",path))==0){
        tryCatch({
          dir.create(path, recursive = TRUE)
        },
        warning=function(w) {
          stop(w)
        })
      }
    }
  }
}

Try the GCSFilesystem package in your browser

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

GCSFilesystem documentation built on Nov. 8, 2020, 7:50 p.m.