I'm creating a module that I want to instantiate with an initial value, but then use what the module itself returns on subsequent uses of the module.
In this dummy example a modal pops up where a slider has a value of 5, but I want the user to be able to move the slider and it "remembers" the users prior selection. Mind you this is just a REPREX I think there's more clever ways to do this, but in my actual app I'm passing in initial data that is manipulated in the module, so I want that updated data to be used when the modal is clicked.... if that makes sense.
Any help on passing reactives around appreciated!!
library(shiny)
mod_ui <- function(id) {
ns <- NS(id)
actionButton(ns("click_me"), "Open for Slider")
}
mod_srv <- function(input, output, session, newValue) {
ns <- session$ns
observeEvent(input$click_me, {
showModal(
modalDialog(
title = "This is a modal",
footer = actionButton(ns("toggle_disable"), "CLOSE MODAL"),
sliderInput(ns("the_slider"), "Slider", min = 0, max = 10, value = newValue)
)
)
})
observeEvent(input$toggle_disable, {
removeModal()
})
return(input$the_slider)
}
ui <- fluidPage(
fluidPage(
mod_ui("module")
)
)
server <- function(input, output) {
# initial state of the reactive values
dataval <- reactiveValues(nV = 5)
# the module uses dataval$nV to originally set the slider to 5
# but on subsequent clicks it should "remember" where it was slided to
temp <- callModule(mod_srv, "module", newValue = dataval$nV)
# lol this doesn't work
observe(temp(), {
dataval$nV <- temp
})
}
shinyApp(ui = ui, server = server)