Executing custom tab in R function

I am currently writing a function that accepts only certain inputs (in the example, only "a" and "b"). For all other inputs, the function will return an error.

test <- function(x) { allowedX <- c("a","b") if(x %in% allowedX) print("Good choice!") else stop("wrong input!") } 

To help users of this function, I would like to provide the permitted values ​​for x (stored in the permitted X) using the tab completion function in R and replace the default file name, which is usually used after the quote. Therefore, pressing TAB should give something like:

 test(x="<TAB> ab 

However, so far I have not been able to find a solution on how to map the allowX vector to the end of the tab in R. Can someone tell me how to do this?

Thanks in advance!

+6
source share
1 answer

You can try something like the following:

 test <- function() { allowedX <- c("a","b") x = readline('Please enter your choice of parameters (either "a" or "b"): ') if(x %in% allowedX) print("Good choice!") else stop("wrong input!") } 
+1
source

Source: https://habr.com/ru/post/949215/


All Articles