0

I'm attempting to add a module to a preexisting R Shiny app to verify users through a cookie based system. To work with cookies in R Shiny, I've followed this tutorial and I've extended shinyjs with the following javascript code:

shinyjs.getcookie = function(params) {
    var cookie = Cookies.get("id");
    if (typeof cookie !== "undefined") {
        Shiny.onInputChange("jscookie", cookie);
    } else {
        var cookie = "";
        Shiny.onInputChange("jscookie", cookie);
    }
}
shinyjs.setcookie = function(params) {
    Cookies.set("id", escape(params), {
        expires: 0.5
    });
    Shiny.onInputChange("jscookie", params);
}
shinyjs.rmcookie = function(params) {
    Cookies.remove("id");
    Shiny.onInputChange("jscookie", "");
}

I made a test example with a small R Shiny app that worked correctly without modules, however, when I tried to implement a module to perform the same function, I ran into an issue retrieving the cookies.

From within server.R, I can retrieve the cookies without issue using the following code:

observe({
  js$getcookie()
  print(input$jscookie)
})

However, when I put that same code into the module's server function, it prints out NULL every time. I believe that the problem has to do with namespaces, and that maybe input$jscookie in server.R refers to a different thing than input$jscookie in the module. However, I'm very new (just a few days) to Shiny modules, so I'm still unsure of some aspects of how they work.

Is there a way to retrieve the cookie from within the module?

J0HN_TIT0R
  • 323
  • 1
  • 13
  • 1
    When you used modules, elements in that module get the name of the module prepended to their id. So maybe your `Shiny.onInputChange("jscookie", params);` is trying to refer to an input that doesn't exist. I suggest using the inspector to see what id the input actually has. – C. Braun Feb 26 '18 at 22:09
  • How would I use the inspector to see what id the input has? I assume you mean the inspector in the browser. – J0HN_TIT0R Feb 26 '18 at 22:20
  • In most browsers you can right-click any element on a webpage and there should be an "inspect" option in the menu. This should then pop up a view of the page's html with the element's markup highlighted. – C. Braun Feb 26 '18 at 22:44

0 Answers0