英文:
Regex for determining if any characters were input besides allowed input, and correcting the input accordingly
问题
我有一个小应用程序,其中包含一个numericInput,旨在仅接受1-96。我希望确保如果输入不是1-96中的任何数字,它将自动更正为1。我已经让特定输入(例如输入等于0、输入等于字母或符号、输入为空格)起作用,但如果可能的话,我想使用一个表达式来捕获这些情况。我的想法是搜索输入并匹配包含除1-96之外的任何字符的字符串。如果它包含除1-96之外的任何内容,那么它将重置输入为1。我对正则表达式不是很擅长,但这是我在Google上搜索几天后的最佳尝试。
我没有收到任何错误消息,但当我使用这个最小可复现示例(MRE)时,当我输入一个字母时,应用程序不会更新num_conds
。当我输入大于96的数字时,它确实会更新num_conds
。当我将此代码插入到我的较大版本中并输入“非法”字符时,应用程序会在没有警告或错误的情况下崩溃。我想知道我的正则表达式是否有问题?有人有什么想法吗?
更新
我还意识到该代码不允许带有0的数字,例如20,并将其重置为1。我将继续努力解决这个问题,但如果有人知道更好的方法,请告诉我。谢谢!
这是我的最小可复现示例(MRE):
#这是为确保只有允许的字符被输入
library(shiny)
library(shinyWidgets)
library(htmlwidgets)
########## 用户界面 ##########
ui = fluidPage(
numericInput("num_conds",
label = "Conditions",
min = 1,
max = 96,
value = 1),
)#关闭用户界面,流式页面
########## 服务器逻辑 #########
server = function(input, output, session) {
observe({
if (any(grepl('[^(?:(?!1-96).)*$]', input$num_conds)) == TRUE) {
freezeReactiveValue(input, "num_conds")
updateNumericInput(session, inputId = "num_conds", value = 1)
}
})
} #关闭服务器
shinyApp(ui = ui, server = server)
如果您需要进一步的帮助,请随时提出。
英文:
I have a small app with a numericInput that is meant to accept only 1-96. I want to make sure that if there is any input that is not 1-96, then it'll automatically correct it to 1. I have gotten this to work for specific inputs (eg input == 0, input == letters or symbols, input == empty space), but I would like to catch this using one expression if possible. My thought is to search the input and match any strings that contain anything that isn't exclusively 1-96. If it does contain something other than only 1-96, then it will reset the input to 1. I'm not great at regex yet, but this is my best attempt after a couple of days on the Googles.
I don't get any errors, but when I use this MRE, the app does not update num_conds
when I input a letter. When I input a number above 96, it does update num_conds
. When I plug this into my larger version and input the 'illegal' characters, the app crashes without a warning or error. I'm wondering if there is something wrong with my regex? Does anyone have any ideas?
UPDATE
I also just realized that the code will not allow a number with a 0, like 20, and will reset to 1. I'll continue to work on this, but please let me know if someone knows better. Thanks!
Here is my MRE:
#This is for ensuring only allowed characters are input
library(shiny)
library(shinyWidgets)
library(htmlwidgets)
########## User interface ##########
ui = fluidPage(
numericInput("num_conds",
label = "Conditions",
min = 1,
max = 96,
value = 1),
)#close ui, fluidpage
########## Server logic #########
server = function(input, output, session) {
observe({
if (any(grepl('[^(?:(?!1-96).)*$]', input$num_conds)) == TRUE) {
freezeReactiveValue(input, "num_conds")
updateNumericInput(session, inputId = "num_conds", value = 1)
}
})
} # close server
shinyApp(ui = ui, server = server)
答案1
得分: 1
不使用正则表达式,您可以使用条件语句,如!input$num_conds %in% 1:96
。
以下是在您的代码上下文中的示例:
# 用于确保只允许输入指定字符的部分
library(shiny)
library(shinyWidgets)
library(htmlwidgets)
########## 用户界面 ##########
ui = fluidPage(
numericInput("num_conds",
label = "条件",
min = 1,
max = 96,
value = 1),
) # 关闭用户界面 fluidpage
########## 服务器逻辑 #########
server = function(input, output, session) {
observe({
if (any(!input$num_conds %in% 1:96) == TRUE) {
freezeReactiveValue(input, "num_conds")
updateNumericInput(session, inputId = "num_conds", value = 1)
}
})
} # 关闭服务器
shinyApp(ui = ui, server = server)
英文:
Instead of regex, you could do a condition like !input$num_conds %in% 1:96
.
Here is what that would look like in the context of your code:
#This is for ensuring only allowed characters are input
library(shiny)
library(shinyWidgets)
library(htmlwidgets)
########## User interface ##########
ui = fluidPage(
numericInput("num_conds",
label = "Conditions",
min = 1,
max = 96,
value = 1),
)#close ui, fluidpage
########## Server logic #########
server = function(input, output, session) {
observe({
if (any(!input$num_conds %in% 1:96) == TRUE) {
freezeReactiveValue(input, "num_conds")
updateNumericInput(session, inputId = "num_conds", value = 1)
}
})
} # close server
shinyApp(ui = ui, server = server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论