实际上,numericInput
接受字符串和数字输入。如果输入字符串,则转换为NA
(尝试使用下面的代码)。有没有办法不允许用户在闪亮的数字字段中键入字符串?
ui <- fluidPage(
numericInput("num", label = "text not allowed", value = 1),
verbatimTextOutput("value")
)
server <- function(input, output) {
output$value <- renderPrint({ input$num })
}
shinyApp(ui = ui, server = server)
到目前为止,我在数字输入旁边添加了一个文本输出,警告用户只有在numericInput
字段中输入字符串时才接受数字。这个解决方案对我来说远非理想。
我希望用户无法在数字字段中输入字符值。
答案 0 :(得分:5)
您可以在表达式中添加validate
,因此只允许输入数字。我正在使用Windows 7 64位谷歌Chrome(IE也可以使用)
注意:Shiny版本0.13.2在Firefox上不起作用。
rm(list = ls())
library(shiny)
ui <- fluidPage(
numericInput("num", label = "text not allowed", value = 1),
verbatimTextOutput("value")
)
server <- function(input, output) {
numbers <- reactive({
validate(
need(is.numeric(input$num), "Please input a number")
)
})
output$value <- renderPrint({ numbers() })
}
shinyApp(ui = ui, server = server)
答案 1 :(得分:1)
IMO是实现这一目标的最简单方法,它将观察者添加到您的输入中,并且当检测到禁止符号时(可能需要您自己的功能检测禁止的符号),只需使用updateInput功能将其删除即可。
更新
observe({
if(is.null(input$myTextInput)) {
vec <- NULL
return()
} else vec <- input$myTextInput
vec <- removeForbiddenSignsFunction(vec)
updateTextInput(session, "myTextInput", value = vec)
})
删除禁止标志功能的示例(此功能会删除Windows文件名中禁止的标志):
removeForbiddenSignsFunction <- function(vec) {
forbidden <- c("|", "?", "*")
notAllowed <- c(">", "<", ":","/"," ")
for(i in 1:length(forbidden)) {
if(grepl(paste0("\\",forbidden[i]),vec)) {
vec <- sub(paste0("\\",forbidden[i]),"",vec)
}
}
for(i in 1:length(notAllowed)) {
if(grepl(notAllowed[i],vec)) {
vec <- sub(notAllowed[i],"",vec)
}
}
if(grepl("\\\\",vec)) vec <- sub("\\\\","",vec)
if(grepl("\"",vec)) vec <- sub("\"","",vec)
return(vec)
}
由于正则表达式特殊符号(正则表达式和Windows文件名中不允许),它被分割为禁止和notAllowed。
答案 2 :(得分:1)
我的解决方案是使用observe()
监视输入,并使用updateNumericInput()
替换不符合要求的参数。
observe({
if (!is.numeric(input$num)) {
updateNumericInput(session, "num", 0)
}
})
答案 3 :(得分:1)
这是一个使用 HTML 而不是 Shiny 的解决方案。解决方案是向 HTML 输入标记添加 pattern
属性。它不会删除不需要的字符,但该字段会变成粉红色,让用户知道输入了无效字符。
1-- 当invalid 被提升时,让背景变成粉红色。为此,我们需要在 CSS 中添加一个样式。在 Shiny 中,这是通过在 ui 中在 style
标签中添加一个 head
标签来实现的,该标签将通过
input[type='text']:invalid
tags$head(
tags$style(HTML("input[type='text']:invalid {background-color: pink;}"))
)
2-- 使用上述内容以及文本输入字段创建 ui,例如:
ui <- fluidPage(
tags$head(
tags$style(HTML("input[type='text']:invalid {background-color: pink;}"))
),
textInput("mySolution", label = "Only letters are valid here", value = ""),
)
3-- 修改这个 ui 以在 input 标签中添加一个 pattern
:
ui <- searchreplaceit(ui, "input", list(id="mySolution"),
"input", list(pattern="[A-Za-z]*"), replace=FALSE)
就是这样!服务器功能不需要任何东西,因为验证都是在页面内执行的。使用
启动页面server <- function(input, output, session) { }
shinyApp(ui = ui, server = server)
这里给出了给标签添加属性的函数
searchreplaceit <- function(branch, whattag, whatattribs, totag, toattribs, replace=TRUE) {
if ("name" %in% names(branch)) {
if ((branch$name == whattag)&&(identical( branch$attribs[names(whatattribs)], whatattribs))) {
branch$name <- totag
branch$attribs <- if (replace) {toattribs} else { modifyList(branch$attribs, toattribs)}
}
}
if ("shiny.tag" %in% class(branch)) {
if (length(branch$children)>0) for (i in 1: length(branch$children)) {
if (!(is.null(branch$children[[i]]))) {
branch$children[[i]] = searchreplaceit(branch$children[[i]], whattag, whatattribs, totag, toattribs, replace)
} }
} else if ("list" %in% class(branch)) {
if (length(branch)>0) for (i in 1:length(branch) ) {
if (!(is.null(branch[[i]]))) {
branch[[i]] <- searchreplaceit(branch[[i]], whattag, whatattribs, totag, toattribs, replace)
} }
}
return(branch)
}
当您不输入字母时: