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

I want my Shiny app (in R) to limit the number of characters a user can enter in response to a textInput command.

I can ask the user to limit to 50 characters and can attach the app to send him a message if he doesn't, but it would be better if he was prevented from exceeding the limit in the first place.

Suggestions appreciated.

+3


source to share


2 answers


You cannot add a custom attribute to textInput, perhaps you can write a custom function to generate input, but in javascript it will be easier:



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

      

+3


source


For example, using packages shinyBS

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

      



Demo page for shinyBS: ShinyBS

0


source







All Articles