rautomationprompt

How to make sure user prompted response equals at least one value from a provided vector?


I am developing a QC check script for my job. I want it to be all within one function, and therefore prompts are asked to the user to determine the arguments used within the function. Here is an example of a splice of function I have developed.

askforQC_type <- \(qctype){
  answer <- readline("What QC types would you like to run?") 
  qualitycontroltypes <- c('BLK', 'DDL', 'DUP', 'FBLK', 'LCS', 'LCSD', 'LDUP', 'LMB', 'MS', 'MSD', 'NO3', 'RNS', 'SO4', 'SPI', 'SPL', 'TBLK')
  if(answer==qualitycontroltypes) {
    writeLines(paste("QC types chosen:", qctype))
  } else {
      writeLines(paste("Sorry,", qctype, "is not an applicable QC type"))
    }
  }

I am wanting the user to provide an answer that at least fits one of the values within the qualitycontroltypes vector. If it does not fit any of those values, I want it to spit out the message. Then, I want to use the values provided by the user to tell the script to paste those values into a subset function later in my script.

Preferable if the user can answer in a format such as BLK, LCS, LDUP, etc. without needing to put "" or c(...) in the answer. Also, if ALL is entered by the user, then run the script using all values in the qualitycontroltypes vector. I am trying to make the script as simple as possible for the user in case they do not know how to operate r at all. Thanks.

UPDATE

As @MrFlick pointed out, the "answer %in% qualitycontroltypes is the correct way to go about this. Now what I need to figure out it how to let the user provide a comma and space before each of the answers until the last answer provided. I think this is what @Limey is getting at in his comment. Not sure how to do this though.


Solution

  • Here is what I think you are looking for with regard to user input:

    askforQC_type <- \(qctype){
      
      assign("qctype",readline("What QC types would you like to run?"))
      qctype <- c(unlist(strsplit(qctype,split = c(","))))
      qctype <- trimws(qctype)
      qualitycontroltypes <- c('BLK', 'DDL', 'DUP', 'FBLK', 'LCS', 'LCSD', 'LDUP', 'LMB', 'MS', 'MSD', 'NO3', 'RNS', 'SO4', 'SPI', 'SPL', 'TBLK')
      if(all(qctype %in% qualitycontroltypes) == TRUE) {
        writeLines(paste("QC types chosen:", qctype))
      } else if(qctype == "ALL"){
        writeLines(paste("QC types chosen:", qualitycontroltypes))
      } else {
        writeLines(paste("Sorry,", qctype, "contains a QC type that is not applicable"))
      }
      
    }
    
    
    askforQC_type()
    
    What QC types would you like to run? DDL,DUP
    
    QC types chosen: DDL
    QC types chosen: DUP
    
    What QC types would you like to run? ALL
    QC types chosen: BLK
    QC types chosen: DDL
    QC types chosen: DUP
    QC types chosen: FBLK
    QC types chosen: LCS
    QC types chosen: LCSD
    QC types chosen: LDUP
    QC types chosen: LMB
    QC types chosen: MS
    QC types chosen: MSD
    QC types chosen: NO3
    QC types chosen: RNS
    QC types chosen: SO4
    QC types chosen: SPI
    QC types chosen: SPL
    QC types chosen: TBLK
    

    What I did was to use the assign() function to assign the user input value(s) to qctype, then split that into a vector string using strsplit() and unlist() and then remove any unwanted whitespace using trimws().

    Then, in the if statement, I used the all() function to check if qctype %in% qualitycontroltypes returns TRUE in case a user supplies more than one input value. If that condition is met, then the program runs as expected.

    I hope this helps!