R/chooseIdentifier.R

Defines functions .chooseIdentifier

Documented in .chooseIdentifier

.chooseIdentifier<-function(func,...,ignore.args=NULL,choose.args=NULL){
  ###################################################################
  #Define the data identifier for the mapping.
  ###################################################################
  #It looks like the function call
  #except that there are no quotes for string arguments
  #And the values of ignore.args arguments is ##IGNORE##
  #And the values of choose.args arguments is ##CHOOSE##
  #The arguments file, level and gdx are not part of the identifier
  #Example: emissions(types=co2_c,y1995=##IGNORE##)
  ###################################################################
  identifier<-.getIdentifier(func,...,ignore.args=ignore.args,choose.args=choose.args,get=FALSE)
  
  ###################################################################
  #Check whether identifier is already present in the mapping for dataset
  ###################################################################
  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<-names(unlist(lapply(mapping,grep,pattern=identifier,fixed=TRUE)))
  ###########################################################################
  #If identifier is not present in the mapping yet, 
  #check if there are similar identifiers that only differ by ##IGNORE## and ##CHOOSE## arguments. 
  ###########################################################################
  if(length(matches)==0){
    #Determine the pattern for potential identifiers
    pot_identifier<-.getIdentifier(func,...,get=TRUE)
    pot_matches<-unlist(lapply(mapping,grep,pattern=pot_identifier,value=T))
    if(length(pot_matches)>0){
      cat("\n\nThere is no entry for ",identifier,"in the mapping yet.")
      cat("\n\nThere are however entries for the following related identifiers.")
      cat("\n\nDo you want to use one of those?.\n\n")
      cat(paste(1:length(pot_matches),pot_matches,sep=": "),sep="\n")
      cat(length(pot_matches)+1,": Stick to ",identifier,"\n")
      cat("\nNumber: ")
      tmp <- get_line()
      tmp<-as.numeric(strsplit(tmp,",")[[1]])
      if(tmp %in%1:length(pot_matches)){
        identifier<-pot_matches[tmp]
      } 
    }
  }
  return(identifier)
}
pik-piam/validation documentation built on Nov. 5, 2019, 12:50 a.m.