如何从闪亮应用程序中访问浏览器会话/ cookie [英] How to access browser session/cookies from within Shiny App

查看:184
本文介绍了如何从闪亮应用程序中访问浏览器会话/ cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何透过Shiny应用程式存取Cookie和其他浏览器相关的工作阶段资料?

How can I access cookies and other browser-related session data from within a Shiny app?

使用 session $ clientData ,我们可以获取其他客户端详细信息,如主机,端口,查询参数...

With session$clientData, we are able to get other client details like host,port,query param...

有没有其他方法可以在闪亮的应用程序中获取cookie?

Is there any other way to get cookies in shiny app?

推荐答案

,您可以使用js.cookie.js包与Shiny.OnInputChange()函数来返回Cookie。

To build on the great comments, you can use the js.cookie.js package with the Shiny.OnInputChange() function to return cookies.

示例应用程序在这里: https://beta.rstudioconnect.com/iwallace/cookies/

An example app is here: https://beta.rstudioconnect.com/iwallace/cookies/

- ui.r -

--ui.r--

library(shiny)
library(shinydashboard)

fluidPage(
tags$head(tags$script(src="js.cookie.js")),
# a shiny element to display unformatted text
box(title ="click the gray square to view cookies!",    verbatimTextOutput("results"),actionButton("go","click me")),

# javascript code to send data to shiny server
tags$script('
          document.getElementById("go").onclick = function() {
          var number = Math.random();

          Cookies.set(\'name\', \'value\', { expires: 7 });
          Cookies.set(\'cookie_2\', \'value\', { expires: 7 });

          var my_cookie = Cookies.get(); 

          Shiny.onInputChange("mydata", my_cookie);
          };
          ')

- server.r -

--server.r--

library(shiny)

shinyServer(function(input, output,session) {
output$results = renderPrint({
input$mydata
})

})

这篇关于如何从闪亮应用程序中访问浏览器会话/ cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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