Action button and selectInput in R Shiny: Passing input of selectInput (multiple = TRUE) to a function only after final selection is made

I have a question regarding R Shiny.

I have defined a function in my global.R that takes 3 arguments and then displays a plot, so:

get.plot(mode, threshold, names = names)

In my Shiny App I have set up a sidebarPanel with a selectInput (multiple = FALSE) for mode, selectInput (multiple = TRUE) for names and a sliderInput for the threshold. In the mainPanel of my ShinyApp I dynamically render the plot using the inputs of the two selectInput objects and the sliderInput, which works perfectly fine.

The names list in my function contains 10 different names (all are shown in default setting) that can be combined in any way using the selectInput (multiple = FALSE). The problem is that my function is quite long and the calculations take long because it recalculates the function each time I delete a name in the selectInput. For example, if I want to display only 2 names, I have to remove 8 names of the selectInput, which means the function recalculates 8 times, which then takes very long.

I thought about showing an action button ("Apply Names") that can be clicked after the names have been selected. However, I still want the plot to adjust dynamically to the other arguments mode and threshold, and the button should only change the names argument.

Do you have any idea how I could solve that? Or do you have any other suggestions?

My setup looks approximately the following:

ui <- fluidPage(

      selectInput(inputId = "network_names",
                  label = "Select multiple names",
                  choices = names,
                  multiple = TRUE,
                  selected = names

      selectInput(inputId = "network_mode",
                  label = "Select a mode",
                  choices = list("Mode 1",
                                 "Mode 2")

      sliderInput(inputId = "threshold",
                  label = "Threshold",
                  min = 1,
                  max = 10,
                  value = 3)

server <- function(input, output){

output$network <- renderPlot({
  if(input$network_mode == "Mode 1") {

    get.plot(1, input$threshold, names = input$network_names)

  } else if(input$network_mode == "Mode 2") {

    get.plot(2, input$threshold, names = input$network_names)

Any help is appreciated. Thank you very much.


Welcome to the RStudio community!

Issues with Shiny code can stem from both the reactive Shiny code itself or the regular R code used in the functions. In order for us to help you with your question, please provide us a minimal reproducible example (Reprex) where you provide a minimal (dummy) dataset and code that can recreate the issue. One we have that, we can go from there. You already provided some of the code, but parts of it are still missing in order for us to run and test it.

For help on creating a Reprex, see this guide:

Good luck!