3

I have code like is in my shiny app:

$(function() {
   Shiny.onInputChange('initialHash', parseHashQuery());
});

function parseHashQuery() {
  var result = {};
  location.hash.replace(/^#/, '').split('&').filter(Boolean).forEach(function(part) {
    var pair = part.split('=');
    result[pair[0]] = pair[1];
  });
  return result;
}

but I've got error:

common.js:59 Uncaught TypeError: Shiny.onInputChange is not a function
    at HTMLDocument.<anonymous> (common.js:59)
    at i (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at Function.ready (jquery.min.js:2)
    at HTMLDocument.K (jquery.min.js:2)
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • FWIW, for parsing hash queries like this you should use `session$clientData$url_search` as per [this](https://stackoverflow.com/questions/32872222/how-do-you-pass-parameters-to-a-shiny-app-via-url) – Carl Sep 22 '17 at 20:36
  • @Carl hash is not sent to the server, url_search is for query string – jcubic Sep 23 '17 at 08:47

1 Answers1

6

The solution was to use shiny event:

  $(document).on('shiny:sessioninitialized', function(event) {
    Shiny.onInputChange('initialHash', parseHashQuery());
  });

which was executed when Shiny.onInputChange was already defined.

Looking at the source code, Shiny is initialized in jQuery ready function but with timeout:

$(function() {
  // Init Shiny a little later than document ready, so user code can
  // run first (i.e. to register bindings)
  setTimeout(initShiny, 1);
});
Carl
  • 5,569
  • 6
  • 39
  • 74
jcubic
  • 61,973
  • 54
  • 229
  • 402