title: In R Shiny, how to flip a function around so it reads a matrix from left-right instead of up-down?
body: <p>The three images below help explain. <em>MWE Code 1</em> reactively interpolates user input values as shown in the 1st image, but the user input matrix needs to instead expand horizontally to the right in pairings of two values to interpolate rather than the vertical (downward) expansion currently used in <em>MWE Code 1</em>. A horizontally expanding matrix with input pairings of two values is shown in the 2nd image and its code in <em>MWE Code 2</em> below. <em>MWE Code 2</em> isn't completely functional like <em>MWE Code 1</em> but it illustrates the desired horizontally-expanding matrix in value pairings of two.</p>
  <p>Note how in <em>MWE Code 2</em> the two input variables to interpolate are “paired” or grouped under a single column heading labelled “Scenario 1”, “Scenario 2”, etc.. This pairing is necessary. A formula for skipping along a matrix that horizontally expands in groupings of two columns is shown in <em>MWE Code 2</em>, with <code>trunc(1:ncol(mm)/2)+1</code>.</p>
  <p>How to modify <em>MWE Code 1</em> so it expands horizontally like <em>MWE Code 2</em>, rather than vertically as it currently does?</p>
  <p>It’s easy enough to change the parameters for the <code>matrixInput</code> function to reorient its expansion and pairings, as done in <em>MWE Code 2</em>; the tricky part is modifying the functions that feed off the matrix especially in the section starting <code>plotData &lt;- reactive({…</code> with its use of <code>lapply...</code>, etc. in <em>MWE Code 1</em>.</p>
  <p>MWE Code 1:</p>
  <pre><code>library(shiny)
  library(shinyMatrix)
  library(dplyr)
  library(ggplot2)
  
  interpol &lt;- function(a, b) { # a = periods, b = matrix inputs
    c &lt;- rep(NA, a)
    c[1] &lt;- b[1]
    c[a] &lt;- b[2]
    c &lt;- approx(seq_along(c)[!is.na(c)], c[!is.na(c)], seq_along(c))$y # this interpolates
    return(c)
  }
  
  ui &lt;- fluidPage(
    sliderInput('periods','Periods to interpolate:',min=2,max=10,value=10),
    matrixInput(
        &quot;myMatrixInput&quot;,
        label = &quot;Values to interpolate (myMatrixInput):&quot;,
        value =  matrix(c(1, 5), 1, 2, dimnames = list(&quot;Scenario 1&quot;, c(&quot;Value 1&quot;, &quot;Value 2&quot;))),
        cols = list(extend = FALSE, names = TRUE, editableNames = FALSE),
        rows = list(names = TRUE,delete = TRUE, extend = TRUE, delta = 1),
        class = &quot;numeric&quot;),
    plotOutput(&quot;plot&quot;)
    )
  
  
  server &lt;- function(input, output, session) {
    
    sanitizedMat &lt;- reactiveVal()
    
    observeEvent(input$myMatrixInput, {
      if(any(rownames(input$myMatrixInput) == &quot;&quot;)){
        tmpMatrix &lt;- input$myMatrixInput
        rownames(tmpMatrix) &lt;- paste(&quot;Scenario&quot;, seq_len(nrow(input$myMatrixInput)))
        updateMatrixInput(session, inputId = &quot;myMatrixInput&quot;, value = tmpMatrix)
      }
      sanitizedMat(na.omit(input$myMatrixInput))
    })
    
    plotData &lt;- reactive({
      req(dim(sanitizedMat())[1] &gt;= 1)
      lapply(seq_len(nrow(sanitizedMat())),
             function(i){
               tibble(
                 Scenario = rownames(sanitizedMat())[i],
                 X = 1:input$periods,
                 Y = interpol(input$periods, sanitizedMat()[i, 1:2])
               )
             }) %&gt;% bind_rows()
    })
    
    output$plot &lt;- renderPlot({
      req(nrow(plotData()) &gt; 0)
      plotData() %&gt;% ggplot() + geom_line(aes(
        x = X,
        y = Y,
        colour = as.factor(Scenario)
      ))
    })
  }
  
  shinyApp(ui, server)
  </code></pre>
  <p>MWE Code 2 (uses same packages and interpol() function as above):</p>
  <pre><code>ui &lt;- fluidPage(
      
      sliderInput('input1','Interpolate over periods (X):',min=2,max=12,value=6),
      matrixInput(&quot;input2&quot;,
                  label = &quot;Input into empty 2nd row cells to add interpolation scenario:&quot;,
                  value = matrix(c(1, 5), 1, 2, dimnames = list(&quot;Begin|end value&quot;, c(&quot;Scenario 1&quot;, &quot;&quot;))),
                  rows =  list(names = TRUE),
                  cols =  list(names = TRUE,
                               extend = TRUE,
                               delta = 2,
                               delete = TRUE,
                               multiheader=TRUE),
                  class = &quot;numeric&quot;),
      actionButton(&quot;add&quot;,&quot;Add scenario&quot;),
      plotOutput(&quot;plot&quot;)
  )
  
  server &lt;- function(input, output, session){
    
    results &lt;- function(){interpol(req(input$input1),req(input$input2))}
    
    numScenarios &lt;- reactiveValues(numS=1)
    
    observeEvent(input$add,{numScenarios$numS &lt;- (numScenarios$numS+1)})
    
    observe({
      req(input$input2)
      mm &lt;- input$input2
      colnames(mm) &lt;- paste(&quot;Scenario &quot;, trunc(1:ncol(mm)/2)+1)
      isolate(updateMatrixInput(session, &quot;input2&quot;, mm))
    })
    
    output$plot &lt;-renderPlot({
      req(input$input1,input$input2)
      v &lt;- lapply(
        1:numScenarios$numS,
        function(i) tibble(Scenario=i,X=1:input$input1,Y=results())
      ) %&gt;%
        bind_rows()
      v %&gt;% ggplot() + 
        geom_line(aes(x=X, y=Y, colour=as.factor(Scenario)))  +
        geom_point(aes(x=X, y=Y))
    })
    
  }
  
  shinyApp(ui, server)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/MxZFt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MxZFt.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/JFzdT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JFzdT.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/1XOmY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1XOmY.png" alt="enter image description here" /></a></p>