我正在构建一个闪亮的应用程序,它可以与我构建的库进行交互。该库中的函数有许多可选参数。函数是编写的,这样它们就可以轻松地处理missing(argument) == TRUE的情况。
如果用户没有指定输入函数,那么它允许用户设置输入的默认值。是否可以将默认值设置为缺失(即不存在)?
例如,这就是如何将默认值设置为NULL,但这不是我想要的。
textInput("argument", label = "Set this optional argument",
value = NULL)编辑:很可能,我应该将函数更改为默认值为NULL,而不是使用missing函数从基设置默认值。尽管如此,我仍然很好奇是否可以将默认值设置为不存在。
发布于 2019-03-05 08:14:33
诀窍是将do.call与一个参数列表一起使用,在该列表中,您为函数放置了所有的arg。然后过滤这个列表以过滤掉“缺失”值。对于textInput中的一个参数,这可能是一个空字符串(. == ""),也可以是确定这是“缺失”输入的任何其他条件。
f <- function(y, x) if (missing(x)) "is missing" else "present"
ui <- fluidPage(textInput("ti", "Set Arg:"),
verbatimTextOutput("out"))
server <- function(input, output) {
output$out <- renderPrint({
## put all your potential args (optional and non optional) in a list
args <- list(y = 1, x = input$ti)
## Filter out NULL values
## define Filter fun which returns TRUE if you consider the value valid
is_valid <- function(.) !is.null(.) && . != ""
## Filter out non valid args
args <- Filter(is_valid, args)
## Use do.call to run your fun (non valid args are not set)
list(inp = input$ti,
out = do.call(f, args))
})
}
shinyApp(ui, server)https://stackoverflow.com/questions/54987424
复制相似问题