Problems storing reactives in session$userData / module Communication / scoping

I am trying to assign a reactive value inside a module. I have two use cases and one seems to work "mod1" and the other "mod2" is not working properly


library(shiny)

mod1UI = function(id){
    ns = NS(id)
    uiOutput(ns("selector"))
}

mod1 = function(input, output, session){
    ns = session$ns 
    output$selector <- renderUI({selectInput(
        ns("selected_table"),
        label = "View Data",
        choices = list(one = "one", two = "two"),
        selected = "one"
    )
    })
    
    session$userData$dataChoice = reactive({req(input$selected_table) })
}

mod2UI = function(id){
    ns = NS(id)
    DT::dataTableOutput(ns("mainTable_choice2"))
}

mod2 = function(input, output, session){
    ns = session$ns
    
    data <- reactive({
        choice = session$userData$dataChoice()
        #print(choice)
        if(choice == "one"){
            dta = iris
        }else if(choice == "two"){
            dta = Orange
        }
    })
    
    
    
    output$mainTable_choice2 = DT::renderDataTable({
        options = list(autoWidth = TRUE, searching = TRUE)
        #browser()
        data_ = data()
        datatable(data_)
    })
    
    
    reactive({
        data = data()
        choice = session$userData$dataChoice()
        if(choice == "one"){
            session$userData$Var1 <- reactive({req(data[input$mainTable_choice2_rows_selected,1])})
        }
    })
    
}
# Define UI for application
ui <- fluidPage(

    # Application title
    titlePanel("Minimum Example"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            mod1UI("mod1")
        ),

        mainPanel(
           mod2UI("mod2")
        )
    )
)

server <- function(input, output, session) {

    callModule(mod1, "mod1")
    callModule(mod2, "mod2")
    
    #swap Var1 for dataChoice and it works !!!
    observeEvent(session$userData$Var1(), {showModal(modalDialog(
        title = "Are you sure",
        textInput("text",label = "test" , value = "testHL02"),
        easyClose = TRUE,
        footer = tagList(
            modalButton("Cancel"),
            actionButton("delete", "Delete")
        )))})
    
}

# Run the application 
shinyApp(ui = ui, server = server)

I can access session$userData$dataChoice() inside an observer and a reactive in another module and obtain a result.

I cant call session$userData$Var1() in another observer. Instead I get the following error:

Error in observeEventExpr: attempt to apply non-function

It seams that i cant assign variables to session$userData while inside reactive({})

This is exemplified when swapping dataChoice with Var1 in the observeEvent in the server function.

Does anyone know why and what to do ?

Please Help,

Thanks
JP

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.