Title: | Use Browser Cookies with 'shiny' |
---|---|
Description: | Browser cookies are name-value pairs that are saved in a user's browser by a website. Cookies allow websites to persist information about the user and their use of the website. Here we provide tools for working with cookies in 'shiny' apps, in part by wrapping the 'js-cookie' JavaScript library <https://github.com/js-cookie/js-cookie>. |
Authors: | Jon Harmon [aut, cre, cph] , Klaus Hartl [cph] (js-cookie JavaScript library), Fagner Brack [cph] (js-cookie JavaScript library) |
Maintainer: | Jon Harmon <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.3.9000 |
Built: | 2024-11-06 13:25:10 UTC |
Source: | https://github.com/r4ds/cookies |
Wrap a shiny ui in this function in order to add cookie-handling
functionality. The ui can be defined in any format compatible with shiny,
using functions such as shiny::fluidPage()
, shiny::bootstrapPage()
,
shiny::htmlTemplate()
, or a raw HTML string.
add_cookie_handlers(ui)
add_cookie_handlers(ui)
ui |
A 0- or 1-argument function defining the ui of a shiny app, or a
|
An object with the same signature as the input ui
, but with the
dependencies needed to handle cookies. If ui
is a shiny::tagList()
, a
shiny::tagList()
will be returned; if ui
is a function, a function will
be returned.
str(add_cookie_handlers("example"))
str(add_cookie_handlers("example"))
Add the js-cookie Javascript library as an HTML dependency, and make cookies available in the shiny input object.
cookie_dependency()
cookie_dependency()
Call this function within your shiny ui to attach the necessary JavaScript code.
An htmltools::htmlDependency()
, which shiny uses to add the
js-cookie Javascript library exactly once.
cookie_dependency()
cookie_dependency()
The shiny request object includes any cookies that are available to the app. This function extracts the value of a named cookie from that request.
extract_cookie(request, cookie_name, missing = NULL)
extract_cookie(request, cookie_name, missing = NULL)
request |
A shiny request object. |
cookie_name |
The name of the cookie. Can contain any US-ASCII characters except for: the control character, space, a tab, or separator characters like ( ) < > @ , ; : \ " / [ ] ? = { }. |
missing |
The value to return if the requested cookie is not stored in
the request. Defaults to |
The contents of that cookie.
req <- list(HTTP_COOKIE = "cookie1=expected_value; cookie2=1; cookie3=2") extract_cookie(req, "cookie1") extract_cookie(req, "cookie2") extract_cookie(list(), "cookie1") extract_cookie(NULL, "cookie1")
req <- list(HTTP_COOKIE = "cookie1=expected_value; cookie2=1; cookie3=2") extract_cookie(req, "cookie1") extract_cookie(req, "cookie2") extract_cookie(list(), "cookie1") extract_cookie(NULL, "cookie1")
The shiny request object includes any cookies that are available to the app. This function extracts those cookies as a named list.
extract_cookies(request)
extract_cookies(request)
request |
A shiny request object. |
All cookies in the request, as a list.
req <- list(HTTP_COOKIE = "cookie1=expected_value; cookie2=1; cookie3=2") extract_cookies(req) extract_cookies(list()) extract_cookies(NULL)
req <- list(HTTP_COOKIE = "cookie1=expected_value; cookie2=1; cookie3=2") extract_cookies(req) extract_cookies(list()) extract_cookies(NULL)
Read a cookie from the input object.
get_cookie( cookie_name, missing = NULL, session = shiny::getDefaultReactiveDomain() )
get_cookie( cookie_name, missing = NULL, session = shiny::getDefaultReactiveDomain() )
cookie_name |
The name of the cookie. Can contain any US-ASCII characters except for: the control character, space, a tab, or separator characters like ( ) < > @ , ; : \ " / [ ] ? = { }. |
missing |
The value to return if the requested cookie does not exist. Defaults to NULL. |
session |
Shiny session in which the cookies can be found (the default should probably always be used). |
A character with the value of the cookie.
server <- function(input, output, session) { get_cookie("my_cookie") }
server <- function(input, output, session) { get_cookie("my_cookie") }
Instruct the user's browser to remove a cookie via JavaScript.
remove_cookie(cookie_name, session = shiny::getDefaultReactiveDomain())
remove_cookie(cookie_name, session = shiny::getDefaultReactiveDomain())
cookie_name |
The name of the cookie. Can contain any US-ASCII characters except for: the control character, space, a tab, or separator characters like ( ) < > @ , ; : \ " / [ ] ? = { }. |
session |
Shiny session in which the cookies can be found (the default should probably always be used). |
A call to session$sendCustomMessage()
which removes the targeted
cookie.
server <- function(input, output, server) { shiny::observeEvent( input$button_that_removes_cookie, remove_cookie("my_cookie") ) }
server <- function(input, output, server) { shiny::observeEvent( input$button_that_removes_cookie, remove_cookie("my_cookie") ) }
Instruct the user's browser to create a cookie via JavaScript.
set_cookie( cookie_name, cookie_value, expiration = 90, secure_only = NULL, domain = NULL, path = NULL, same_site = NULL, session = shiny::getDefaultReactiveDomain() )
set_cookie( cookie_name, cookie_value, expiration = 90, secure_only = NULL, domain = NULL, path = NULL, same_site = NULL, session = shiny::getDefaultReactiveDomain() )
cookie_name |
The name of the cookie. Can contain any US-ASCII characters except for: the control character, space, a tab, or separator characters like ( ) < > @ , ; : \ " / [ ] ? = { }. |
cookie_value |
The contents of the cookie as a single character value. |
expiration |
Days after which the cookie should expire. To remove an HttpOnly cookie, send a negative value for this attribute. |
secure_only |
Logical indicating whether the cookie should only be
accessible via secure ( |
domain |
The host to which the cookie will be sent (including
subdomains). If this is |
path |
The path that must exist in the requested URL for the browser to send this cookie. Includes subdirectories. |
same_site |
One of "strict", "lax" (default), or "none", indicating when
the cookie should be sent. When |
session |
Shiny session in which the cookies can be found (the default should probably always be used). |
A call to session$sendCustomMessage()
which sets the targeted
cookie.
server <- function(input, output, server) { shiny::observeEvent( input$button_that_sets_cookie, set_cookie( "my_cookie", "the value of this cookie" ) ) }
server <- function(input, output, server) { shiny::observeEvent( input$button_that_sets_cookie, set_cookie( "my_cookie", "the value of this cookie" ) ) }
Generate a shiny::tagList()
which uses JavaScript to set a cookie in the
user's browser when the shiny app loads.
set_cookie_on_load( cookie_name, cookie_value, expiration = 90, secure_only = NULL, domain = NULL, path = NULL, same_site = NULL )
set_cookie_on_load( cookie_name, cookie_value, expiration = 90, secure_only = NULL, domain = NULL, path = NULL, same_site = NULL )
cookie_name |
The name of the cookie. Can contain any US-ASCII characters except for: the control character, space, a tab, or separator characters like ( ) < > @ , ; : \ " / [ ] ? = { }. |
cookie_value |
The contents of the cookie as a single character value. |
expiration |
Days after which the cookie should expire. To remove an HttpOnly cookie, send a negative value for this attribute. |
secure_only |
Logical indicating whether the cookie should only be
accessible via secure ( |
domain |
The host to which the cookie will be sent (including
subdomains). If this is |
path |
The path that must exist in the requested URL for the browser to send this cookie. Includes subdirectories. |
same_site |
One of "strict", "lax" (default), or "none", indicating when
the cookie should be sent. When |
A shiny::tagList()
that provides the HTML and javascript to set the
cookie.
set_cookie_on_load("my_cookie", "contents of my cookie") set_cookie_on_load("my_cookie", "contents of my cookie", expiration = 10)
set_cookie_on_load("my_cookie", "contents of my cookie") set_cookie_on_load("my_cookie", "contents of my cookie", expiration = 10)
Send a shiny::httpResponse()
that sets a cookie in the user's browser. Note
that this does not return a full shiny ui.
set_cookie_response( cookie_name, cookie_value, expiration = 90, secure_only = NULL, domain = NULL, path = NULL, same_site = NULL, http_only = FALSE, redirect = NULL, ... )
set_cookie_response( cookie_name, cookie_value, expiration = 90, secure_only = NULL, domain = NULL, path = NULL, same_site = NULL, http_only = FALSE, redirect = NULL, ... )
cookie_name |
The name of the cookie. Can contain any US-ASCII characters except for: the control character, space, a tab, or separator characters like ( ) < > @ , ; : \ " / [ ] ? = { }. |
cookie_value |
The contents of the cookie as a single character value. |
expiration |
Days after which the cookie should expire. To remove an HttpOnly cookie, send a negative value for this attribute. |
secure_only |
Logical indicating whether the cookie should only be
accessible via secure ( |
domain |
The host to which the cookie will be sent (including
subdomains). If this is |
path |
The path that must exist in the requested URL for the browser to send this cookie. Includes subdirectories. |
same_site |
One of "strict", "lax" (default), or "none", indicating when
the cookie should be sent. When |
http_only |
Logical indicating whether the cookie should only be sent as
part of an HTTP request. When this is |
redirect |
A relative or absolute URL where the user should be sent next. A typical case would be the same URL minus the query parameter that triggered the Set-cookie response. |
... |
Additional parameters passed on to |
A shiny::httpResponse()
that sets the cookie.
set_cookie_response("my_cookie", "contents of my cookie") set_cookie_response("my_cookie", "contents of my cookie", expiration = 10) set_cookie_response( "my_cookie", "contents of my cookie", content = "Your cookie is set." ) set_cookie_response( "my_cookie", "contents of my cookie", redirect = "/" )
set_cookie_response("my_cookie", "contents of my cookie") set_cookie_response("my_cookie", "contents of my cookie", expiration = 10) set_cookie_response( "my_cookie", "contents of my cookie", content = "Your cookie is set." ) set_cookie_response( "my_cookie", "contents of my cookie", redirect = "/" )