gpt4 book ai didi

r - R 函数中的自定义选项卡完成

转载 作者:行者123 更新时间:2023-12-04 15:59:05 26 4
gpt4 key购买 nike

我目前正在编写一个只接受某些输入的函数(在示例中只有“a”和“b”)。对于所有其他输入,该函数将返回错误。

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

为了帮助该函数的用户,我想使用 R 中的制表符补全功能为 x(存储在 allowedX 中)提供允许的值,并替换通常在引号后应用的默认文件名补全。所以按 TAB 应该给出类似的内容:
test(x="<TAB>
a b

但是,到目前为止,我找不到如何将向量 allowedX 映射到 R 中的选项卡完成的解决方案。有人可以告诉我该怎么做吗?

提前致谢!

最佳答案

您可以尝试以下操作:

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!")
}

关于r - R 函数中的自定义选项卡完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17592018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com