我正在尝试从闪亮的应用程序中的用户浏览器中检索cookie,以便为不同的用户生成不同的UI。为了使用cookie sin R shiny,我已经跟着this tutorial并且我使用以下javascript代码扩展了shinyjs:
shinyjs.getcookie = function(params) {
var cookie = Cookies.get("id");
if (typeof cookie !== "undefined") {
Shiny.onInputChange("jscookie", cookie);
} else {
var cookie = "";
Shiny.onInputChange("jscookie", cookie);
}
}
shinyjs.setcookie = function(params) {
Cookies.set("id", escape(params), {
expires: 0.5
});
Shiny.onInputChange("jscookie", params);
}
shinyjs.rmcookie = function(params) {
Cookies.remove("id");
Shiny.onInputChange("jscookie", "");
}
我也在尝试在R Shiny模块中设置和获取cookie,这可能很重要。
我可以在模块中设置cookie,我可以从我的浏览器验证这个;但是,当我尝试获取cookie时,我一直都是NULL。
我在server.R中调用模块,如下所示:
user_auth_return <- observe({
callModule(
user_auth_module,
"user_auth"
)
})
这是模块本身:
user_auth_module <- function(input, output, session) {
print("1")
# Initializing reactive values.
values <- reactiveValues(
passwords = data.frame(matrix(nrow = 0, ncol = 2)),
cookies = data.frame(matrix(nrow = 0, ncol = 2)),
cookies.match = FALSE,
user = NULL,
password.error = FALSE
)
# Setting new cookie.
observeEvent(input$login, {
print("5")
# Reading in pasword table.
try(values$passwords <- read.csv("data/passwords.csv", header = FALSE, quote = "", stringsAsFactors = FALSE))
# Checking if match exists for username-password pair.
if (input$username != "" &&
input$username %in% values$passwords[, 1] &&
checkpw(input$password, hash = values$passwords[values$passwords[, 1] == input$username, 2])) {
print("6")
# Creating random session ID.
sessionid <- paste(
collapse = '',
sample(x = c(letters, LETTERS, 0:9), size = 64, replace = TRUE)
)
sessionid <- "cookie123"
# Setting new cookie to session ID.
print("setting cookie")
js$setcookie(sessionid)
# Updating user cookie if already present, otherwise, creating new username-cookie pair.
if (input$username %in% values$cookies[, 1]) {
print("7")
values$cookies[values$cookies[, 1] == input$username, 2] <- sessionid
} else {
print("8")
values$cookies <- rbind(values$cookies, c(input$username, sessionid))
}
# Writing updated cookie data frame to data file.
write.table(values$cookies, file = "data/cookies.csv", quote = FALSE, sep = ",", row.names = FALSE, col.names = FALSE)
} else {
print("9")
values$password.error <- TRUE
}
})
# Checking if match exists for cookie.
observe({
# Check cookies when input is pressed.
input$login
# Reading in cookie table.
try(values$cookies <- read.csv("data/cookies.csv", header = FALSE, quote = "", stringsAsFactors = FALSE))
# Getting cookie.
js$getcookie()
print("user")
print(input$jscookie)
print("database")
print(values$cookies[, 2])
# Checking if match exists for cookie.
if (!is.null(input$jscookie) &&
input$jscookie %in% values$cookies[, 2]) {
print("3")
values$cookies.match <- TRUE
values$user <- values$cookies[values$cookies[, 2] == input$jscookie, 1]
} else {
print("4")
values$cookies.match <- FALSE
}
})
# Toggling password error message if username/password combination is not found.
observe({
print("10")
toggleState("password.error.message", condition = values$password.error)
})
# Toggling between login and logout tab if cookie is present.
observeEvent(values$cookie.match, {
print("11")
hide("login_page")
show("logout_page", anim = TRUE, animType = "fade")
})
# Remove user cookie at logout.
observeEvent(input$logout, {
print("12")
js$rmcookie()
})
# Returning login status.
return("return")#values$cookies.match)
}
当我运行模块时,我得到以下数字,显示每个打印语句中程序的进展:
[1] "1"
[1] "user"
NULL
[1] "database"
[1] "cookie123"
[1] "4"
[1] "10"
输入正确的密码后:
[1] "5"
[1] "6"
[1] "setting cookie"
[1] "7"
[1] "user"
NULL
[1] "database"
[1] "cookie123"
[1] "4"
感谢任何帮助。
编辑:
我发现我能够使用以下代码从server.R中检索cookie:
observe({
js$getcookie()
print("server cookie")
print(input$jscookie)
})
因为我仍然无法使用模块中的相同代码访问cookie,我认为我已将问题范围缩小到变量input$jscookie
。与Shiny服务器相比,我是否必须在Shiny模块中以不同的方式获得此值?