eflores89
eflores89

Reputation: 359

Passing reactive server values to modules

I have a shiny app with server function like so;

shinyServer(function(input, output) {
 chosen_fam <- reactive({ input$fam })

  output$newui <- renderUI({  NewProductUI("newproduct", fam = chosen_fam()) })
  output$newbtn <- renderUI({ NewProductButtonUI("newproduct", fam =  chosen_fam()) })

  callModule(NewProduct, id = "newproduct", fam =  chosen_fam())
})

Inside of the NewProduct module there is something like this:

NewProduct <- function(input, output, session, fam){
  observeEvent(input$submitnew, {
    print(paste0("Adding.... ", fam))

    row <- c(fam, input$variable)
    upload_to_googlesheets(token, row)
})

The UI is rendered depending on the chosen family and the server module also does different things depending on the family.

Rendering UI seems to work fine, but the "fam" variable in the callModule does not update (hence it is always the result of the first chosen family). Variables from the inputs are going thru fine, so I imagine this has something to do with scoping (it might be looking for "fam" in the "newproduct" namespace?).

How do I explicitly tell callModule to use the reactive expression from the session namespace? Or is there a way I can just add the fam variable to the "newproduct" namespace?

Upvotes: 0

Views: 939

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84539

You have to give the reactive conductor itself:

callModule(NewProduct, id = "newproduct", fam =  chosen_fam)

(no parenthesis). And in the module:

row <- c(fam(), input$variable)

(parentheses).

Upvotes: 4

Related Questions