将Cookie读取到具有嵌入式javascript的Shiny应用程序 [英] Read in cookie to Shiny application with embedded javascript

查看:63
本文介绍了将Cookie读取到具有嵌入式javascript的Shiny应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Shiny并喜欢它.但是我遇到了一个绊脚石,在我的Shiny应用程序中加入了javascript以添加一些附加功能.希望能获得帮助.

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.

这是一个非常基本的Shiny应用程序,我正在使用它来测试使用javascript读取浏览器cookie的可行性,以便可以在ui.R中对其进行访问.

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.

ui.R代码.

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

'script'标记随附的javascript函数-取自quirksmode.org/js/cookies.html.

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;
}

和服务器代码.

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

首先,我应该问一下是否有可能将Cookie读入闪亮的应用程序中?第二,我在正确的轨道上吗?第三-假设我的js代码运行正常-如何在其源代码的闪亮代码中访问js函数返回的值?

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?

任何人以及所有帮助者都赞赏并提出建设性的批评.我是新手,因此欢迎提供任何有助于将Shiny和JS整数化的指针.

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

推荐答案

Shinyjs 可能是解决此问题的方法.

Shinyjsis probably the way tog go with this.

以下是不使用Shinyjs即可读取Cookie的方法:

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)

运行

document.cookie="username=John Doe";

在浏览器控制台中创建cookie.

In your browser console to create a cookie.

这篇关于将Cookie读取到具有嵌入式javascript的Shiny应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆