javascript - 使用嵌入的 javascript 将 cookie 读入 Shiny 应用程序

标签 javascript cookies shiny

我正在开始使用 Shiny 并且喜欢它。但在将 javascript 合并到我的 Shiny 应用程序中以添加一些附加功能时,我遇到了一个障碍。希望能得到一些帮助。

这是一个非常基本的 Shiny 应用程序,我用它来测试使用 javascript 读取浏览器 cookie 的可行性,以便可以在 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。

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 读入 Shiny 的应用程序?其次,我现在走的路正确吗?第三 - 假设我的 js 代码工作正常 - 我如何在其来源的 Shiny 代码中访问 js 函数返回的值?

任何和所有的帮助都值得赞赏,建设性的批评也是如此。我是新人,所以欢迎任何有助于整合 Shiny 和 JS 的指示。

最佳答案

Shinyjs可能是解决这个问题的方法。

以下是如何在不使用shinyjs的情况下读取cookie的方法:

# 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。

关于javascript - 使用嵌入的 javascript 将 cookie 读入 Shiny 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880361/

相关文章:

javascript - 如何将动画 list 按钮放置在文本右侧

javascript - 不明白为什么 forEach 无法访问其循环的数组

javascript - 禁用位置 :fixed on scrolling when 250px from bottom of the page

c# - 在 ASP.NET MVC 中创建 Cookie

r - ggvis - 条形图的交互式 X 轴

javascript - 如何使用 htmlOutput 在 R Shiny 应用程序中启用语法突出显示

javascript - Require.JS 是否会阻止访问者查看/复制外部脚本?

cookies - 隔离同级域的同站点 cookie

r - 如何更改 Shiny 仪表板标题的颜色和字体?

session - session 和cookie之间的关系