How to limit the number of characters accepted by textInput in Shiny app

别等时光非礼了梦想. 提交于 2019-12-01 03:41:12

You can't add custom attribute to textInput, you could probably be able to write custom function to generate input, but it will be easier to do this in javascript:

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

For example by using shinyBS and stringr packages:

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")

Demo page for shinyBS:ShinyBS

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!