1

For the dateInput funciton in R Shiny, how do we embed the JavaScript and make the datepicker autoclose?

I found in another JS post that the option is

$('#datepicker').datepicker({
autoclose: true,
});

How can I add this function to R Shiny? Thanks!

StatCC
  • 285
  • 3
  • 11

1 Answers1

1

I came up with a polling solution. Try to save this javascript code as a .js file and put it into the Shiny app, at the very end of body. datepickerId is the id of your datepicker field, according to your R code. Here is how to include javascript files into shiny.

var datepickerId = "myDate",
updateDatepicker = function(){
obj = $("div#"+datepickerId+" .form-control.datepicker");
if(obj.data().hasOwnProperty("datepicker")){
obj.on('changeDate', function (ev) {
     $(this).datepicker('hide');
});
} else {
window.setTimeout(updateDatepicker(),100);
}
};
updateDatepicker();
// alternative: window.onload = function(){ updateDatepicker(); }; 
Community
  • 1
  • 1
nilsole
  • 1,663
  • 2
  • 12
  • 28
  • I add `tags$head(tags$script(src = "datepicker.js"))` in ui.R script and change datepickerId to the id in `dateInput` function. Where should I add the `autoclose` option?(I know nothing about javascript...) Thanks! – StatCC Oct 06 '16 at 21:37
  • As I stated in my code, you might consider using `window.onload = function(){ updateDatepicker(); };`instead of `updateDatepicker();` as your last line. That depends on how your app works. – nilsole Oct 07 '16 at 08:59