rshinymoduleselectinput

How do I get the value of an selectInput in another Module in R shiny?


First I read my data in and create an Event Log

data <- read.csv2(file = "Data/FX_test.csv", stringsAsFactors = FALSE, na.strings=c(""))

df_use <- data

df_use$Timestamp = as.POSIXct(df_use$Timestamp, format = "%Y-%m-%d %H:%M:%S")

#Create Event Log
events <- bupaR::activities_to_eventlog(
    df_use,
    case_id = 'Case.ID',
    activity_id = 'Activity',
    resource_id = 'User',
    timestamp = c('Timestamp', 'Timestamp')
)

then I create an UI and a Server for my SelectInput

updateUI <- function(id, list_act){
    ns <- NS(id)
    selectInput(ns("act"), h5(strong(em("ACTIVITIES"))), choices = NULL, multiple = TRUE)
}

updateServer <- function(id, my_x){
    moduleServer(id, function(input, output, session){
        #update ui-selectInput "act" with existing Activities
        act.opts <- unique(my_x)
        updateSelectInput(session, "act", choices = c("All", act.opts), selected = "All")
    }
    )
}

Another module with UI and Server for buttons Here I want to use the output of the SelectInput-Value from above. But I always get an error. I also tried to put it in a reactive or observeEvent() but both didn't help.

actionButtonUI <- function(id){
    ns <- NS(id)
    actionButton(ns("PLOT"), label = "PLOT", style = "color: #fff; background-color: #337ab7;border-color: #2e6da4")
}

actionButtonServer <- function(id){
    moduleServer(id, function(input, output, session){
        observeEvent(input$PLOT, {
            print("Button Check") 
            
            act <- input$act1    ### I want this value. This does not work ###
            print(act)
            
        })
        
    })
        
}

And the final UI and Server that puts all together.

ui <- fluidPage(
    updateUI('act_1'),
    actionButtonUI('button1')
   
)

server <- function(input, output) {
    updateServer('act_1', df_use$Activity)
    actionButtonServer('button1')


}

shinyApp(ui = ui, server = server)

Could someone please help. Thank you.


Solution

  • You need to return the value from the updateServer to the main app and pass it to the actionButtonServer:

    updateServer <- function(id, my_x){
      moduleServer(id, function(input, output, session){
        #update ui-selectInput "act" with existing Activities
        act.opts <- unique(my_x)
        updateSelectInput(session, "act", choices = c("All", act.opts), selected = "All")
        
        return(list(act = reactive({input$act})))
      }
      )
    }
    
    actionButtonServer <- function(id, act_input){
      moduleServer(id, function(input, output, session){
        observeEvent(input$PLOT, {
          print("Button Check") 
          
          act <- act_input$act()
          print(act)
          
        })
        
      })
      
    }
    
    server <- function(input, output) {
      act_input <- updateServer('act_1', df_use$Activity)
      actionButtonServer('button1', act_input)
      
      
    }
    

    For an introduction, have a look at my tutorial: https://github.com/jonas-hag/structure_your_app/