I have a shiny app with a text input box. I want to limit the number of characters that can be entered and display a note below the box saying how many characters remain. I'm not sure how I would prevent more characters than the limit from being entered. The code I have below, if it worked, would only display how many characters remain but there's nothing stopping it from being negative.
As a less preferable solution, I was trying to have a modal box pop up if the action button is clicked and the input box has more than the character limit, but I couldn't get that to work either.
ui <- fluidPage(title = "App Title",
dashboardPage(title = "App Title",
dashboardBody(tabItems(tabItem(tabName = 'Tab1',
fluidRow(uiOutput('comment_UI'),
actionButton('upload_comment', 'Upload Comment')
),
helpText(paste0('Characters remaining:', textOutput('charcount')))
)
)
)
)
)
server <- function(input, output, session) {
output$comment_UI <- renderUI({textareaInput('comment','Comment')})
output$charcount <- renderText({800-nchar(input$comment)})
observeEvent(input$upload_comment, {
if(nchar(input$comment) <= 800) {
[do stuff] }
else {
showModal(modalDialog(title = "Error",
"The character limit has been exceeded.")) }
}
}