Driving me crazy - this little app for managing cookies works fine except that the first time a row in the table is selected, it immediately gets unselected. I'm sure it is something simple I am missing...
#
#
library(tidyverse)
library(shiny)
library(cookies)
state="TX"
# Define UI for application that draws a histogram
ui <- add_cookie_handlers(
fluidPage(
# Application title
titlePanel("Read and Write cookie"),
# Sidebar
sidebarLayout(
sidebarPanel(
textInput("cookieText", "Text for cookie", "---"),
actionButton(
"addCookie",
"Add cookie"
),
HTML("<br/>"),
"Selected row",
textOutput("sel_cookie"),
actionButton(
"delCookie",
"Delete cookie"
)
),
# Show a table
mainPanel(
DT::dataTableOutput("cookies")
)
)
)
)
# Define server logic
server <- function(input, output) {
############## Add cookie
observeEvent(
input$addCookie,
{
Old_cookie <- get_cookie(state)
if (!is.null(Old_cookie)) {
Value <- paste(Old_cookie, input$cookieText, sep=",")
} else {
Value <- input$cookieText
}
set_cookie(
cookie_name = state,
cookie_value = Value
)
}
)
############## Delete cookie
observeEvent(
input$delCookie,
{
Old_cookie <- get_cookie(state)
if (is.null(Old_cookie)) {
return()
}
print(paste("delCookie", input$cookies_rows_selected, Old_cookie))
Cookie_table <- Cookie_table() %>%
filter(!row_number() %in% input$cookies_rows_selected)
set_cookie(
cookie_name = state,
cookie_value = paste(Cookie_table$Value, collapse = ",")
)
}
)
########## cookie dataframe
Cookie_table <- reactive({
print("--10--")
Cookie <- get_cookie(state)
print(paste("--2--", Cookie))
if (is.null(Cookie)) {
return(data.frame(Value="None"))
}
data.frame(Value=Cookie) %>%
mutate(Value=stringr::str_split(Value, ",")) %>%
unnest(Value)
})
###### cookie table
output$cookies <- DT::renderDataTable({
print("--11--")
Cookie_table()
}, selection = 'single')
############# Table selection controls
observeEvent(input$cookies_rows_selected, ignoreNULL = FALSE, {
print(paste("--3--", input$cookies_rows_selected))
output$sel_cookie <- renderText({input$cookies_rows_selected})
}) #END OBSERVE EVENT
}
# Run the application
shinyApp(ui = ui, server = server#,
# Cookies only work in an actual browser.
# options = list(
# launch.browser = TRUE
# )
)