title: How to move a linked object into modal dialog?
body: <p>The below code works as intended, bug-free. Matrix 1, including any user input into matrix 1, is linked to (&quot;downstreams to&quot;) matrix 2 as &quot;Scenario 1&quot;, and the user can add scenarios via matrix 2. Results are plotted. Additional scenarios input into matrix 2 are shown as additional lines in the plot. For sake of illustration, all matrix inputs are simply summed and plotted over 10 periods via the <code>sumMat()</code> function.</p>
  <p>Any ideas for how to move matrix 2 into modal dialog? The user would optionally input into matrix 2 after clicking the single <code>actionButton()</code> in the code. Matrix 1 remains in the <code>sidebarPanel()</code>. While maintaining the exact same functionality that the code currently has, whereby the plot reactively updates as the user inputs into either matrix 1 or matrix 2 and where both matrices remain linked?</p>
  <pre><code>library(dplyr)
  library(ggplot2)
  library(shiny)
  library(shinyMatrix)
  
  sumMat &lt;- function(x){return(rep(sum(x,na.rm = TRUE), 10))}
  
  ui &lt;- fluidPage(
    sidebarLayout(
      sidebarPanel(
        matrixInput(&quot;matrix1&quot;,
                    label =&quot;Matrix 1 (scenario 1):&quot;,
                    value = matrix(c(60,5), nrow = 1, ncol = 2, dimnames = list(NULL,c(&quot;X&quot;,&quot;Y&quot;))),
                    rows = list(extend = TRUE, delete = TRUE),
                    class = &quot;numeric&quot;),
        actionButton(inputId = &quot;showMat2&quot;, &quot;Add scenarios&quot;),br(),br(),
        matrixInput(&quot;matrix2&quot;,
                    label = &quot;Matrix 2:&quot;,
                    value = matrix(c(60,5), ncol = 2, dimnames = list(NULL, rep(&quot;Scenario 1&quot;, 2))),
                    rows = list(extend = TRUE, delete = TRUE),
                    cols = list(extend = TRUE, delta = 2, delete = TRUE, multiheader = TRUE),
                    class = &quot;numeric&quot;)
        ),
        mainPanel(plotOutput(&quot;plot&quot;))
    )
  )
  
  server &lt;- function(input, output, session){
    
    observeEvent(input$matrix1, {
      a &lt;- apply(input$matrix2,2,'length&lt;-',max(nrow(input$matrix2),nrow(input$matrix1)))
      b &lt;- apply(input$matrix1,2,'length&lt;-',max(nrow(input$matrix2),nrow(input$matrix1)))
      c &lt;- if(length(a) == 2){c(b)} else {c(b,a[,-1:-2])}
      d &lt;- ncol(input$matrix2)
      tmpMat2 &lt;- matrix(c(c), ncol = d)
      colnames(tmpMat2) &lt;- paste(&quot;Scenario&quot;,rep(1:ncol(tmpMat2),each=2,length.out=ncol(tmpMat2)))
  
      if(any(rownames(input$matrix1) == &quot;&quot;)){
        tmpMat1 &lt;- input$matrix1
        rownames(tmpMat1) &lt;- paste(&quot;Row&quot;, seq_len(nrow(input$matrix1)))
        updateMatrixInput(session, inputId = &quot;matrix1&quot;, value = tmpMat1)
        }
      updateMatrixInput(session, inputId = &quot;matrix2&quot;, value = tmpMat2)
    })
    
    observeEvent(input$matrix2, {
      if(any(colnames(input$matrix2) == &quot;&quot;)){
        tmpMat2 &lt;- input$matrix2
        colnames(tmpMat2) &lt;- paste(&quot;Scenario&quot;,rep(1:ncol(tmpMat2),each=2,length.out=ncol(tmpMat2)))
        updateMatrixInput(session, inputId = &quot;matrix2&quot;, value = tmpMat2)
        }
      if(any(rownames(input$matrix2) == &quot;&quot;)){
        tmpMat2 &lt;- input$matrix2
        rownames(tmpMat2) &lt;- paste(&quot;Row&quot;, seq_len(nrow(input$matrix2)))
        updateMatrixInput(session, inputId = &quot;matrix2&quot;, value = tmpMat2)
        }
      input$matrix2
    })
    
    observeEvent(input$showMat2,{
      showModal(
        modalDialog(
          h5(&quot;Matrix 2 needs to be shown here, with user ability to input into matrix 2&quot;),
          h5(&quot;User inputs into matrix 1 would automatically downstream to the 2 left-most columns of matrix 2 as Scenario 1&quot;),
          footer = tagList(modalButton(&quot;Close&quot;))
        ))
    })
    
    plotData &lt;- reactive({
      tryCatch(
        lapply(seq_len(ncol(input$matrix2)/2), 
               function(i){
                 tibble(
                   Scenario= colnames(input$matrix2)[i*2-1],X=seq_len(10),
                   Y=sumMat(input$matrix2[,(i*2-1):(i*2), drop = FALSE])
                 )
               }) %&gt;% bind_rows(),
        error = function(e) NULL
      )
    })
    
    output$plot &lt;- renderPlot({
      req(plotData())
      plotData() %&gt;% ggplot() + 
        geom_line(aes(x = X, y = Y, colour = as.factor(Scenario))) +
        theme(legend.title=element_blank())
    })
  }
  
  shinyApp(ui, server)
  </code></pre>