Skip to content

Instantly share code, notes, and snippets.

@xiaodaigh
Created October 25, 2013 22:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xiaodaigh/7162506 to your computer and use it in GitHub Desktop.
Save xiaodaigh/7162506 to your computer and use it in GitHub Desktop.
R Shiny Password Input
library(shiny)
shinyServer(function(input, output, session) {
# Partial example
output$meh <- renderPrint({
print("Meh --- ")
print(input$myTextInput )
print(input$passInput )
})
})
library(shiny)
passwordTextInput <- function (inputId, label, value = "") {
tagList(tags$label(label, `for` = inputId), tags$input(id = inputId,
type = "password", value = value,class="passTextInput"))
}
code = HTML(" <script> var myTextInputBinding = new Shiny.InputBinding();
$.extend(myTextInputBinding, {
find: function(scope) {
return $(scope).find('.passTextInput');
},
getId: function(el) {
//return InputBinding.prototype.getId.call(this, el) || el.name;
return $(el).attr('id')
},
getValue: function(el) {
return el.value;
},
setValue: function(el, value) {
el.value = value;
},
subscribe: function(el, callback) {
$(el).on('keyup.textInputBinding input.textInputBinding', function(event) {
callback()
});
},
unsubscribe: function(el) {
$(el).off('.myTextInputBinding');
},
receiveMessage: function(el, data) {
if (data.hasOwnProperty('value'))
this.setValue(el, data.value);
if (data.hasOwnProperty('label'))
$(el).parent().find('label[for=' + el.id + ']').text(data.label);
$(el).trigger('change');
},
getState: function(el) {
return {
label: $(el).parent().find('label[for=' + el.id + ']').text(),
value: el.value
};
},
getRatePolicy: function() {
return {
policy: 'debounce',
delay: 250
};
}
});
Shiny.inputBindings.register(myTextInputBinding, 'shiny.myTextInput');</script>")
shinyUI(
basicPage(
code
,textInput("myTextInput","My text input")
,passwordTextInput("passInput","My pass input")
,textOutput("meh")
,HTML('<script src="https://gist.github.com/xiaodaigh/7162506.js"></script>')
))
@yihui
Copy link

yihui commented Oct 26, 2013

I guess you can avoid copying the input binding code by extending the existing shiny.textInput binding, e.g.

var passwordBinding = $.extend({}, Shiny.inputBindings.bindingNames['shiny.textInput'].binding, {
  find: function(scope) {
    return $(scope).find('.passTextInput');
  }
})

Since the major thing you want to modify is the find method (there may be a couple other places that you need to modify as well).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment