title: R Shiny - Extracting Anti-Diagonal elements in matrix using for-loops
body: <p>I am trying to create an R Shiny app which can read matrix inputs and extract the anti-diagonal elements, however, I can't figure out why the codes don't work as the way I wanted.</p>
  
  <p>Below are the sample codes:</p>
  
  <pre><code>library(shinyMatrix)
  library(shiny)
  library(shinydashboard)
  library(shinyWidgets)
  library(shinyjs)
  library(rhandsontable)
  library(matrixStats)
  
  ui =
    dashboardPage(
      dashboardHeader(disable = TRUE),
      dashboardSidebar(disable = TRUE),
      dashboardBody(rHandsontableOutput("input1"),
                    br(),
                    rHandsontableOutput("input2"),
                    br(),
                    rHandsontableOutput("results")))
  
  
  server = function (input, output, session) {
  
    output$input1 = renderRHandsontable({
  
      MAT = matrix(as.numeric(''), nrow = 3, ncol = 3,
                   dimnames = list(paste(1:3), paste(1:3)))
  
      rhandsontable(MAT, width = "100%", height = "100%") %&gt;%
        hot_col(col = c(1:3), valign = 'htCenter', format = "0,0")
  
  
    })
  
    row_input &lt;- reactive({
      req(input$input1)
  
      my_input_matrix &lt;- as.matrix(hot_to_r(input$input1))
      my_input_row&lt;- as.matrix(hot_to_r(input$input1))
  
      for(i in 1:3) {
  
        my_input_row[i] = sum(my_input_matrix[,i])
  
  
      }
  
  
      row_input = matrix(my_input_row, nrow = 1, ncol = 3,
                          dimnames = list("Rowname", paste(1:3)))
      row_input
    })
  
  
  
  
    output$input2 &lt;- renderRHandsontable({
  
      rhandsontable(row_input())
  
    })
  
  
  table &lt;- reactive({ 
  
    my_input_matrix &lt;- as.data.frame(hot_to_r(input$input1))
    my_input_row &lt;- as.data.frame(hot_to_r(input$input2))
    my_table &lt;- as.data.frame(hot_to_r(input$input1),
                              hot_to_r(input$input2))
  
  
    for(i in 1:3) {
      for(j in 3:1) {
  
        my_table[,1] &lt;- my_input_matrix[j,i] 
        my_table[,2] &lt;- my_input_matrix[i,j]
  
      }
    }
  
  
    table = data.frame("A" = my_table[,1],
                       "B" = my_table[,2],
                       stringsAsFactors = FALSE,
                       check.names = FALSE)
  
  
  
    table
  })
  
  output$results = renderRHandsontable({
  
    rhandsontable(table()) 
  
  })
  
  }
  
  shinyApp(ui, server)
  </code></pre>
  
  <p>Below is the sample inputs and outputs:</p>
  
  <p>1st table is the input matrix</p>
  
  <p>2nd table is a 1-row output matrix which shows the sum of each column of the 1st table(not sure if this causes the issue, so I'll just put it there)</p>
  
  <p>3rd table is the output table produced by the codes</p>
  
  <p><a href="https://i.stack.imgur.com/psWzB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/psWzB.png" alt="enter image description here"></a></p>
  
  <p>Here is the issue, I want the 3rd table to show the anti-diagonal elements 7-5-3 in column A and "reverse anti-diagonal" elements 3-5-7 in column B from the 1st table like below instead of the above (3-3-3- and 7-7-7).</p>
  
  <p><a href="https://i.stack.imgur.com/QwnEk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QwnEk.png" alt="enter image description here"></a></p>
  
  <p>Please help! Thanks!</p>