Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check_path_for_data() #46

Open
DanChaltiel opened this issue Jun 25, 2024 · 0 comments
Open

check_path_for_data() #46

DanChaltiel opened this issue Jun 25, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@DanChaltiel
Copy link
Owner

Maybe start to use the fs package?

In read_tm:

check_path_for_data(project_name, dl_path)
check_path_for_data(project_name, "data", ask=FALSE, advice=FALSE)

Function code:

user_download_path = function(){
  normalizePath(file.path(Sys.getenv("USERPROFILE"), "Downloads"), winslash="/")
}

#add to tests: 
exists('cli_menu', where='package:cli', mode='function') %>% `!` %>% stopifnot()

#' @source https://github.com/r-lib/cli/issues/228#issuecomment-1453614104
cli_menu <- function(prompt, not_interactive, choices, quit = integer(), .envir = caller_env()) {
  if (!cli:::is_interactive()) {
    cli::cli_abort(c(prompt, not_interactive), .envir = .envir)
  }
  choices <- sapply(choices, cli::format_inline, .envir = .envir, USE.NAMES = FALSE)
  
  choices <- paste0(seq_along(choices), " ", choices)
  cli::cli_inform(
    c(prompt, choices),
    .envir = .envir
  )
  
  repeat {
    selected <- readline("Selection: ")
    if (selected %in% c("0", seq_along(choices))) {
      break
    }
    cli::cli_inform("Enter an item from the menu, or 0 to exit")
  }
  
  selected <- as.integer(selected)
  if (selected %in% c(0, quit)) {
    cli::cli_abort("Quiting...", call = NULL)
  }
  selected
}


dl_path = path.expand("~/../Downloads")
dl_path = user_download_path()

path = dl_path
path = "data"
archive = "data/XXXX_ExportTemplate_2020_12_31_SAS_XPORT_2024_06_10_11_56.zip"
project_name = archive %>% basename() %>% str_extract("(.*)_Export", group=TRUE)

#use package fs?
check_path_for_data = function(project_name, path, ask=TRUE, advice=NULL){
  files = dir(path, pattern=project_name, full.names=TRUE)
  
  files_dates = EDCimport:::parse_file_datetime(files)
  max_date = max(files_dates, na.rm=TRUE)
  
  if(max_date > datetime_extraction){
    date_diff = round(max_date - datetime_extraction)
    last_file = files[files_dates==max_date]
    last_file_short = basename(last_file)
    
    cli_inform("There is a database in {.path {path}} that is {date_diff} days more recent than the current extraction ({date_extraction}).")
    
    from=last_file
    to=paste0("data/", last_file_short)
    user_input = 2
    if(ask){
      user_input = cli_menu("Would you like to copy it to {.path /data}?", choices=c("Yes", "No"))
      if(user_input==1){
        ok=file.copy(from, to, copy.date=TRUE)
        if(!ok) stop("Error during copy")
      }
    } 
    if(isTRUE(advice) || is.null(advice) && user_input==2){
      cli_inform(c("Use the following code to copy it to {.path data}:",
                   " "='{.emph  file.copy("{from}", \n"{to}", \ncopy.date=TRUE)}'))
    }
    
  }
  
}
@DanChaltiel DanChaltiel added the enhancement New feature or request label Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant