【发布时间】:2015-01-26 16:27:30
【问题描述】:
我希望我的 Shiny 应用程序(在 R 中)限制用户可以输入的字符数以响应 textInput 命令。
我可以要求用户限制为 50 个字符,如果他不这样做,可以让应用程序向他发送消息,但如果他首先被阻止超出限制会更好。
建议表示赞赏。
【问题讨论】:
-
向我们展示代码。也许我们可以帮助您改善这一点。
标签: shiny textinput character-limit
我希望我的 Shiny 应用程序(在 R 中)限制用户可以输入的字符数以响应 textInput 命令。
我可以要求用户限制为 50 个字符,如果他不这样做,可以让应用程序向他发送消息,但如果他首先被阻止超出限制会更好。
建议表示赞赏。
【问题讨论】:
标签: shiny textinput character-limit
你不能为 textInput 添加自定义属性,你可能可以编写自定义函数来生成输入,但是在 javascript 中这样做会更容易:
shinyjs::runjs("$('#inputName').attr('maxlength', 50)")
【讨论】:
shinyjs:: 调用是服务器端的,但实际的限制执行是由客户端浏览器完成的,这应该是)。
maxlength 在 shiny。
例如使用shinyBS 和stringr 包:
library(stringr)
library(shinyBS)
string <- "Destrier ipsum dolor cold weirwood, consectetur adipisicing elit, sed full of terrors incididunt green dreams always pays his debts. Ut in his cups sandsilk, no foe may pass spearwife nisi ut aliquip we do not sow. Duis aute warrior feed it to the goats death before disgrace maidenhead dog the seven pariatur. Rouse me not cupidatat non proident, suckling pig culpa qui officia deserunt mollit we light the way."
observe({
if(str_length(string)>50) {
newstring <-str_sub(string, end=50)
createAlert(session, inputID = "alert_anchor",
message = "You exceeded 50 character limit!",
dismiss = TRUE,
block = FALSE
append = TRUE)
updateTextInput(session, inputID, value = newstring)
}
})
# remember to create alert to shiny UI
# bsAlert(inputID = "alert_anchor")
shinyBS 的演示页面:ShinyBS
【讨论】: