R/chooseCollection.R

Defines functions .chooseCollection

Documented in .chooseCollection

.chooseCollection<-function(collections=NULL){
  print(names(valdata$data))
  get_line <- function(){
    # gets characters (line) from the terminal of from a connection
    # and stores it in the return object
    if(interactive()){
      s <- readline()
    } else {
      con <- file("stdin")
      s <- readLines(con, 1, warn=FALSE)
      on.exit(close(con))
    }
    return(s);
  }
  matches<-match(collections,names(valdata$data))
  matches<-matches[which(!is.na(matches))]
  if(length(matches)==0){
    cat("\n\nThe requested data collection doesn't exist yet.")
    cat("\nYou can either choose one of the existing collections to store in or create a new one:\n\n")
    cat("1: New collection\n")
    cat("2: Cancel\n")
    cat(paste(3:(length(names(valdata$data))+2),names(valdata$data),sep=": "),sep="\n")
    cat("\nNumber: ")
    newdata <- get_line()
    newdata<-as.numeric(strsplit(newdata,",")[[1]])
    if(newdata==1){
      if(length(collections)>0){
        cat("\n\nUnder which name do you want to save the data?\n")
        for(i in 1:length(collections)){
          cat(i,": ",collections[i],"\n")
        }
        cat(length(collections)+1,": New name\n")
        tmp <- get_line()
        tmp<-as.numeric(strsplit(tmp,",")[[1]])
      } else {
        tmp<-1
      }
      if(tmp ==length(collections)+1){
        cat("Choose something meaningful but not too long, e.g. 'cropland_physical'.\n")
        cat("\nName of the data collection: ")
        collection <- get_line()
      } else if(tmp %in% 1:length(collections)){
        collection<-collections[tmp]
      } else {
        return("unknown")
      }
    } else if(newdata%in% 3:(length(names(valdata$data))+2)) {
      collection<-names(valdata$data)[newdata-2]
    } else {
      return("unknown")
    }
    cat("\n\nThe collection you chose is ",collection,".\n\n")
    cat("Do you want to continue saving the dataset under this name?\n\n")
    cat("1: Yes","2: No",sep="\n")
    check<-get_line()
    if(check!=1){
      return("unknown")
    }
  } else if(length(matches)==1){
    collection<-names(valdata$data)[matches]
  } else {
    cat("\n\nMore than one collection corresponds to your criteria\n\n")
    cat("Under which name do you want to save the data?\n\n")
    cat(paste(1:length(matches), names(valdata$data)[matches], sep=": " ),sep="\n")
    cat("\nNumber: ")
    colnr <- get_line()
    colnr<-as.numeric(strsplit(colnr,",")[[1]])
    if(colnr %in% 1:length(matches)){
      collection<-names(valdata$data)[matches[colnr]]
    } else {
      return(unknown)
    }
  }
  return(collection)
}
pik-piam/validation documentation built on Nov. 5, 2019, 12:50 a.m.