10

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

我可以要求用户限制为 50 个字符,如果他不这样做,可以让应用程序向他发送消息,但如果他首先被阻止超出限制会更好。

建议表示赞赏。

4

2 回答 2

8

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

shinyjs::runjs("$('#inputName').attr('maxlength', 50)")
于 2017-03-31T14:27:07.677 回答
1

例如通过使用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 的演示页面:ShinyBS

于 2014-11-28T07:02:50.890 回答