Shiny - How to replace chars in textInput to *** without using passwordInput?

Viewed 29

I use textInput in my shinyapp. When the user write I want to see - "****" - like password field, but I don't want to use passwordInput, How can I do it with using only textInput ?

if (interactive()) {
  
  ui <- fluidPage(
    textInput("caption", "Caption", "Data Summary"),
    verbatimTextOutput("value")
  )
  server <- function(input, output) {
    output$value <- renderText({ input$caption })
  }
  shinyApp(ui, server)
}
2 Answers

You could define a custom function:

textInputStars <- function(inputId, label, value = "", width = NULL,
                      placeholder = NULL) {
  
  value <- restoreInput(id = inputId, default = value)
  
  div(class = "form-group shiny-input-container",
      style = css(width = validateCssUnit(width)),
      shinyInputLabel(inputId, label),
      tags$input(id = inputId, type="password", class="form-control", value=value,
                 placeholder = placeholder)
  )
}

Based on: https://github.com/rstudio/shiny/blob/main/R/input-password.R

Note that this implementation would be almost identical to passwordInput (see: https://github.com/rstudio/shiny/blob/main/R/input-password.R), besides that input would be restored.

Update without passwordInput() Adapted from heres TextInputIcon (from Shinywidgets) with password entry

library(shiny)

passwordInput_custom <-
  '<div class="form-group shiny-input-container">
        <label class="control-label" id="txt_password-label" for="txt_password"></label>
          <i class="fa fa-user" role="presentation" aria-label="user icon"></i>
        </span>
      <input id="txt_password" type="password" class="form-control" value=""/>
    </div>'

ui <- fluidPage(tagList(HTML(passwordInput_custom)),
                textOutput('psswd'))

server <- function(input, output, session) {
  output$psswd <- renderText({
    input$txt_password
  })
  
}

shinyApp(ui, server)

enter image description here

First answer: Use passwordInput()

From https://shiny.rstudio.com/reference/shiny/1.6.0/passwordInput.html

library(shiny)

ui <- fluidPage(
  passwordInput("password", "Password:"),
  actionButton("go", "Go"),
  verbatimTextOutput("value")
)
server <- function(input, output) {
  output$value <- renderText({
    req(input$go)
    isolate(input$password)
  })
}
shinyApp(ui, server)

enter image description here

Related