## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = FALSE ) library(shinyGovstyle) ## ----------------------------------------------------------------------------- # library(shiny) # library(shinyGovstyle) # # ui <- gov_page( # shinyjs::useShinyjs(), # shinyGovstyle::header( # org_name = "Example", # service_name = "User Examples" # ), # shinyGovstyle::cookieBanner("User Examples"), # shinyGovstyle::gov_main_layout(), # shinyGovstyle::footer(full = TRUE) # ) # # server <- function(input, output, session) { # # Show the relevant confirmation message and hide the main banner # observeEvent(input$cookieAccept, { # shinyjs::show(id = "cookieAcceptDiv") # shinyjs::hide(id = "cookieMain") # }) # # observeEvent(input$cookieReject, { # shinyjs::show(id = "cookieRejectDiv") # shinyjs::hide(id = "cookieMain") # }) # # # Hide the whole banner once the user has read the confirmation # observeEvent(input$hideAccept, shinyjs::toggle(id = "cookieDiv")) # observeEvent(input$hideReject, shinyjs::toggle(id = "cookieDiv")) # # # input$cookieLink takes users to a dedicated cookies page. Here the page is # # a hidden tabsetPanel, so we switch to it with shiny::updateTabsetPanel(). # observeEvent(input$cookieLink, { # updateTabsetPanel(session, "tab-container", selected = "panel-cookies") # }) # } # # if (interactive()) shinyApp(ui, server) ## ----------------------------------------------------------------------------- # mod_cookies_ui <- function(id) { # shiny::tagList( # shinyGovstyle::heading_text("Cookies", size = "l", level = 1), # shinyGovstyle::gov_text( # "We use analytics cookies to measure how the service is used so we", # "can improve it." # ), # shiny::uiOutput(shiny::NS(id, "cookie_saved")), # shinyGovstyle::radio_button_Input( # inputId = shiny::NS(id, "cookies_analytics"), # label = "Do you want to accept analytics cookies?", # choices = c("Yes" = "yes", "No" = "no"), # selected = "no", # inline = TRUE # ), # shinyGovstyle::button_Input( # shiny::NS(id, "save_cookies"), # "Save cookie settings" # ) # ) # } ## ----------------------------------------------------------------------------- # mod_cookies_server <- function(id, cookie_accept, cookie_reject, active_tab) { # shiny::moduleServer(id, function(input, output, session) { # # Banner choices drive the settings radio without the user touching it. # # ignoreInit = TRUE stops these firing on startup. # shiny::observeEvent( # cookie_accept(), # shinyGovstyle::update_radio_button_Input( # session, # inputId = "cookies_analytics", # selected = "yes" # ), # ignoreInit = TRUE # ) # # shiny::observeEvent( # cookie_reject(), # shinyGovstyle::update_radio_button_Input( # session, # inputId = "cookies_analytics", # selected = "no" # ), # ignoreInit = TRUE # ) # # # Saving shows a success banner reflecting the live radio value. # saved_choice <- shiny::reactiveVal(NULL) # # shiny::observeEvent(input$save_cookies, { # saved_choice( # if (identical(input$cookies_analytics, "yes")) "accept" else "reject" # ) # }) # # # Clear the success banner when the user leaves the cookies tab, so a # # stale message isn't waiting for them when they come back. # shiny::observeEvent( # active_tab(), # { # if (!identical(active_tab(), "panel-cookies")) { # saved_choice(NULL) # } # }, # ignoreInit = TRUE # ) # # output$cookie_saved <- shiny::renderUI({ # choice <- saved_choice() # if (is.null(choice)) { # return(NULL) # } # shinyGovstyle::noti_banner( # session$ns("saved_banner"), # title_txt = "Success", # body_txt = paste0( # "You've updated your cookie preferences. You chose to ", # choice, # " analytics cookies." # ), # type = "success" # ) # }) # }) # } ## ----------------------------------------------------------------------------- # server <- function(input, output, session) { # mod_cookies_server( # "cookies", # cookie_accept = shiny::reactive(input$cookieAccept), # cookie_reject = shiny::reactive(input$cookieReject), # active_tab = shiny::reactive(input[["tab-container"]]) # ) # # # Banner show/hide handlers (as in the banner example above) # observeEvent(input$cookieAccept, { # shinyjs::show(id = "cookieAcceptDiv") # shinyjs::hide(id = "cookieMain") # }) # observeEvent(input$cookieReject, { # shinyjs::show(id = "cookieRejectDiv") # shinyjs::hide(id = "cookieMain") # }) # observeEvent(input$hideAccept, shinyjs::toggle(id = "cookieDiv")) # observeEvent(input$hideReject, shinyjs::toggle(id = "cookieDiv")) # # # The "View cookies" link in the banner opens the cookies tab # observeEvent(input$cookieLink, { # updateTabsetPanel(session, "tab-container", selected = "panel-cookies") # }) # }