Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie with checkboxes #81

Closed
Estateira opened this issue Jun 18, 2024 · 1 comment
Closed

Cookie with checkboxes #81

Estateira opened this issue Jun 18, 2024 · 1 comment

Comments

@Estateira
Copy link

Estateira commented Jun 18, 2024

Hello,

following the MWE on the ReadMe page on github, I created a minimal shiny app with one slider and one checkbox, whereby in both
cases cookies are set and retrieved via observeEvent, the problem is that after I reload the app everything works fine for the sliderInput but for the checkbox the value is constantly set to TRUE after reload, even though I do not click it.

library(cookies)
library(shiny)

ui <- add_cookie_handlers(
fluidPage(
titlePanel("A Simple App"),
fluidRow(
sliderInput(
"number_selector",
label = paste(
"Select a number.",
"This selector sets the cookie value.",
"It also initializes with the cookie value.",
"Refresh to see it remembered.",
sep = "\n"
),
min = 1,
max = 10,
value = 1
), checkboxInput("check", "Checkbox", value = FALSE))))

server <- function(input, output, session) {
observeEvent(
input$number_selector,
{
set_cookie(
cookie_name = "selected_number",
cookie_value = input$number_selector ) } )

observeEvent(
get_cookie("selected_number"),
updateSliderInput(
inputId = "number_selector",
value = get_cookie("selected_number")
),
once = TRUE )

observeEvent(
input$check,
{
set_cookie(
cookie_name = "check_cookie",
cookie_value = input$check ) })

observeEvent(
get_cookie("check_cookie"),
updateSliderInput(
inputId = "check",
value = get_cookie("check_cookie")
),
once = TRUE)}

shinyApp(ui, server, options = list( launch.browser = TRUE ))

@jonthegeek
Copy link
Contributor

@Estateira Thank you for this report! I'm sorry it took me so long to get back to you; I managed to lose this report in my inbox, and finally got to it today!

There are two issues here:

  1. You have a typo in your observeEvent(get_cookie("check_cookie"), ...). You copy/pasted updateSliderInput, and need updateCheckboxInput().
  2. More importantly, cookies are text. The values of the checkbox are saved as "true" and "false". I'm trying to decide whether I should handle that inside the package, or just make it more explicit in help. Either way it's something for me to sort out! I think I'll probably save more than just the value in the cookie, and coerce the value back to its original class when it comes back, but I need to think about whether it's worth that effort vs telling users how to deal with it themselves. See Cookie with checkboxes #81.

In any case, thank you for bringing this to my attention!

This is a working version of your app:

library(cookies)
library(shiny)

ui <- add_cookie_handlers(
  fluidPage(
    titlePanel("A Simple App"),
    fluidRow(
      sliderInput(
        "number_selector",
        label = paste(
          "Select a number.",
          "This selector sets the cookie value.",
          "It also initializes with the cookie value.",
          "Refresh to see it remembered.",
          sep = "\n"
        ),
        min = 1,
        max = 10,
        value = 1
      ),
      checkboxInput("check", "Checkbox", value = FALSE)
    )
  )
)

server <- function(input, output, session) {
  observeEvent(
    input$number_selector,
    {
      set_cookie(
        cookie_name = "selected_number",
        cookie_value = input$number_selector
      ) 
    }
  )
  
  observeEvent(
    get_cookie("selected_number"),
    updateSliderInput(
      inputId = "number_selector",
      value = get_cookie("selected_number")
    ),
    once = TRUE
  )
  
  observeEvent(
    input$check,
    {
      set_cookie(
        cookie_name = "check_cookie",
        cookie_value = input$check
      ) 
    }
  )
  
  observeEvent(
    get_cookie("check_cookie"),
    updateCheckboxInput(
      inputId = "check",
      value = as.logical(get_cookie("check_cookie"))
    ),
    once = TRUE
  )
}

shinyApp(ui, server, options = list( launch.browser = TRUE ))

BTW, if you don't already have something similar, I find the Cookie-Editor Chrome extension extremely helpful when working on things that involve cookies! It took me a while to sort out what was happening in this case, but seeing "true" and "false" instead of TRUE and FALSE eventually got through to me!

@jonthegeek jonthegeek closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants