如何限制Shiny app中textInput接受的字符数

时间:2014-11-28 03:21:48

标签: shiny textinput character-limit

我希望我的Shiny app(在R中)限制用户可以输入的字符数以响应textInput命令。

我可以请求用户限制为50个字符,并且可以让应用程序向他发送消息,如果他没有,但如果他被阻止首先超出限制会更好。

建议表示赞赏。

2 个答案:

答案 0 :(得分:4)

您无法向textInput添加自定义属性,您可能可以编写自定义函数来生成输入,但在javascript中执行此操作会更容易:

shinyjs::runjs("$('#inputName').attr('maxlength', 50)")

答案 1 :(得分:0)

例如,使用shinyBSstringr包:

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