限制闪亮字段中的输入类型
Restrict input type in shiny field
实际上,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
字段中输入字符串,则只接受数字。这个解决方案对我来说远非理想。
我希望用户无法在数字字段中输入字符值。
您可以将 validate
添加到您的表达式中,因此只允许输入数字。我正在使用 windows 7 64 位和 Google Chrome(IE 也可以)
注意:Shiny 版本 0.13.2 不适用于 Firefox。
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)
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 文件名中不允许),它被拆分为 forbidden 和 notAllowed。
我的解决方案是使用 observe()
来监视输入,如果它不符合要求的参数,则使用 updateNumericInput()
替换它。
observe({
if (!is.numeric(input$num)) {
updateNumericInput(session, "num", 0)
}
})
这是一个使用 HTML 而不是 Shiny 的解决方案。解决方案是向 HTML 输入标签添加一个 pattern
属性。它不会删除不需要的字符,但该字段会变成粉红色,例如让用户知道输入了无效字符。
1-- 引发invalid时让背景变成粉红色。为此,我们需要在 CSS 中添加一个样式。在 Shiny 中,这是通过在 head
标签中添加 ui 一个 style
标签来实现的,该标签将附加到 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在输入标签中添加一个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)
}
中给出了相关版本
当您不输入字母时:
实际上,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
字段中输入字符串,则只接受数字。这个解决方案对我来说远非理想。
我希望用户无法在数字字段中输入字符值。
您可以将 validate
添加到您的表达式中,因此只允许输入数字。我正在使用 windows 7 64 位和 Google Chrome(IE 也可以)
注意:Shiny 版本 0.13.2 不适用于 Firefox。
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)
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 文件名中不允许),它被拆分为 forbidden 和 notAllowed。
我的解决方案是使用 observe()
来监视输入,如果它不符合要求的参数,则使用 updateNumericInput()
替换它。
observe({
if (!is.numeric(input$num)) {
updateNumericInput(session, "num", 0)
}
})
这是一个使用 HTML 而不是 Shiny 的解决方案。解决方案是向 HTML 输入标签添加一个 pattern
属性。它不会删除不需要的字符,但该字段会变成粉红色,例如让用户知道输入了无效字符。
1-- 引发invalid时让背景变成粉红色。为此,我们需要在 CSS 中添加一个样式。在 Shiny 中,这是通过在 head
标签中添加 ui 一个 style
标签来实现的,该标签将附加到 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在输入标签中添加一个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)
}
当您不输入字母时: