识别Shiny Server中的用户Web浏览器信息

Mar*_*ark 6 r shiny shiny-server

在Shiny Server中,我可以通过获取有关客户端的一些信息session$clientData.在我目前正在使用的项目中

parseQueryString(session$clientData$url_search)
Run Code Online (Sandbox Code Playgroud)

获取URL中的参数.我希望能够获取网络浏览器信息(Google收集的用于分析的信息类型).虽然我可能想知道如何在ASP.NET或PHP可能的JS中做到这一点,但我并不清楚如何在我自己的JS中编织一个Shiny应用程序并将结果信息传递给变量.

如何获取随网页请求一起发送的浏览器信息?

Hal*_*wan 0

这是一个使用此答案中的 JS 函数的小示例。从评论来看,听起来该解决方案并不完美,但却是一个很好的起点。

library(shiny)

js <- "
// execute the code after the shiny session has started
$(document).on('shiny:sessioninitialized', function(event) {
  // browser detection from /sf/answers/414315401/
  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)
Run Code Online (Sandbox Code Playgroud)