2

I'm getting started with Shiny and loving it. But I've come upon a stumbling point with incorporating javascript in my Shiny applications to add on some additional functionality. Hoping to get some help.

Here's a very basic Shiny application I'm using to test the feasibility of reading in a browser cookie with javascript so it can be accessed in ui.R.

The ui.R code.

# UI file of getCookie Shiny application.
shinyUI(fluidPage(
        titlePanel('Cookie'),
        cookie <- tags$head(tags$script(src='readCookie.js')),
        print(cookie)
))   

The javascript function included with the 'script' tag - taken from quirksmode.org/js/cookies.html.

function readCookie() {
        name = "raisinCookie";
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return      c.substring(nameEQ.length,c.length);
        }   
        return null;
}

And the server code.

# Server file of getCookie Shiny application. 
shinyServer(function(input, output){
})

First off I should ask if it's even possible to read cookies in to a shiny application? Second, am I on the right track here? Third - assuming my js code was working properly - how could I access the value returned by a js function within the shiny code it's sourced in?

Any and all help appreciated, constructive criticism as well. I'm new, so any pointers to help w/ integerating Shiny and JS are welcome.

Dunc
  • 23
  • 5
  • 1
    In regards to your question of how to easily integrate javascript with shiny, you might want to look at [shinyjs](https://github.com/daattali/shinyjs) package, specifically the `extendShinyjs()` function which lets you call your own javascript functions from R. Take a look, it might be useful – DeanAttali Nov 24 '15 at 00:15

2 Answers2

3

Shinyjsis probably the way tog go with this.

Here's how you can read in a cookie without using shinyjs:

# UI file of getCookie Shiny application.
ui <- shinyUI(fluidPage(
  titlePanel('Cookie'),
  tags$head(tags$script(
    HTML('
      Shiny.addCustomMessageHandler ("readCookie",function (message) {
        var cookie = readCookie(message.name);
        Shiny.onInputChange("cookie", cookie);
      })

      function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(";");
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==" ") c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return      c.substring(nameEQ.length,c.length);
        }   
        return "No such cookie";
      }   
      ')
    )),
  sidebarPanel(
    actionButton('readCookie','Get Cookie'),
    textInput('cookieName','Name of cookie: ')
    ),
  mainPanel(
    textOutput('cookieVal')
  )
)) 

# Server file of getCookie Shiny application. 
server <- shinyServer(function(input, output,session){
  observeEvent(input$readCookie,{
    session$sendCustomMessage(type="readCookie",
                              message=list(name=input$cookieName))
  })

  observeEvent(input$cookie,{
    output$cookieVal <- renderPrint({ input$cookie })
  })
})

shinyApp(ui=ui, server=server)

Run

document.cookie="username=John Doe";

In your browser console to create a cookie.

RmIu
  • 4,357
  • 1
  • 21
  • 24
1

This is an example app of how to retrieve all cookies using the js.cookies.js library

https://beta.rstudioconnect.com/iwallace/cookies/

The code is part of previous answer How to access browser session/cookies from within Shiny App

Community
  • 1
  • 1
Iain
  • 1,608
  • 4
  • 22
  • 27