如何从Shiny应用程序中访问cookie和其他与浏览器相关的会话数据?
使用session $ clientData,我们可以获得其他客户端详细信息,如主机,端口,查询参数...
有没有其他方法可以在闪亮的应用程序中获取cookie?
为了建立在很棒的评论之上,您可以使用带有 Shiny.OnInputChange() 函数的 js.cookie.js 包来返回 cookie。
示例应用程序在这里:https : //beta.rstudioconnect.com/iwallace/cookies/
--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);
};
')
Run Code Online (Sandbox Code Playgroud)
)
--server.r--
library(shiny)
shinyServer(function(input, output,session) {
output$results = renderPrint({
input$mydata
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4645 次 |
| 最近记录: |