6

In Shiny Server I can get some information about the client via session$clientData. In my current project I'm using

parseQueryString(session$clientData$url_search)

To get parameters in the URL. I want to be able to get the web browser information as well (the kind of information Google collects for analytics). While I could probably figure out how to do this in ASP.NET or PHP possibly JS, I'm not really clear on how exactly to weave in my own JS to a Shiny app and pass the resulting information to a variable.

How can I get the browser information that's sent along with the request for the webpage?

Mark
  • 4,387
  • 2
  • 28
  • 48
  • 1
    If you can do it with JS, [here](https://shiny.rstudio.com/articles/communicating-with-js.html) is an article explaining how to use it in Shiny. – vladli Jul 18 '19 at 11:17

1 Answers1

0

Here's a small example using the JS function from this answer. From the comments, it sounds like the solution isn't perfect, but is a good starting point.

library(shiny)

js <- "
// execute the code after the shiny session has started
$(document).on('shiny:sessioninitialized', function(event) {
  // browser detection from https://stackoverflow.com/a/5918791/8099834
  navigator.sayswho= (function(){
    var ua= navigator.userAgent, tem, 
    M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\\/))\\/?\\s*(\\d+)/i) || [];
    if(/trident/i.test(M[1])){
        tem=  /\\brv[ :]+(\\d+)/g.exec(ua) || [];
        return 'IE '+(tem[1] || '');
    }
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\\b(OPR|Edge)\\/(\\d+)/);
        if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
    }
    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem= ua.match(/version\\/(\\d+)/i))!= null) M.splice(1, 1, tem[1]);
    return M.join(' ');
  })(); 
  // pass browser info from JS to R
  Shiny.onInputChange('myBrowser', navigator.sayswho); 
});
"

ui <- fluidPage(
  tags$head(
      tags$script(HTML(js))
  ),
  textOutput("myBrowserOutput")
)

server <- function(input, output, session) {

  output$myBrowserOutput <- renderText({
      input$myBrowser # contains the value returned by the JS function
  })

}

shinyApp(ui, server)
Hallie Swan
  • 2,714
  • 1
  • 15
  • 23