R shiny matrixinput with editable number of rows and columns

I have this sort of "code" which inherently does not work as I cannot find a guide I can follow about matrixinput and rshiny on its own. Essentially, what I am hoping to create is a matrix input whose number of rows and columns can be changed through numeric inputs. How should I go about this?

library("shiny")
library("shinyMatrix")

textInput("rows", "Number of rows:", "")
textInput("columns", "Number of columns:", "")

ui <- fluidPage(
  titlePanel("shinyMatrix: Simple App"),
  sidebarPanel(
    width = 6,
    numericInput("rows", "Number of rows:", ""),
    numericInput("columns", "Number of columns:", ""),
  ),
  
  mainPanel(
    matrixInput("matrix1",
      value = m,
      rows = list(
        names = TRUE,
        editableNames = TRUE),
      cols = list (
        names = TRUE,
        editableNames = TRUE
      ),
      class = "numeric"),
    width = 6,
  )
)

server <- function(input, output, session) {
  m <- matrix(0, nrow = input$rows, ncol = input$columns)
}

shinyApp(ui, server)
library("shiny")
library("shinyMatrix")

ui <- fluidPage(
  titlePanel("shinyMatrix: Simple App"),
  sidebarPanel(
    width = 6,
    numericInput("rows", "Number of rows:", ""),
    numericInput("columns", "Number of columns:", ""),
  ),
  
  mainPanel(
   uiOutput("changing_mat"),
    width = 6
  )
)

server <- function(input, output, session) {
 
  output$changing_mat <- renderUI({
    ir <- input$rows
    ic <- input$columns
    validate(
      need(isTruthy(ir),label = "rows"),
      need(isTruthy(ic),label = "columns"),
    )
    matrixInput("matrix1",
                value = matrix(data=0,
                               nrow=ir,
                               ncol=ic),
                rows = list(
                  n=ir,
                  names = TRUE,
                  editableNames = TRUE),
                cols = list (
                  n=ic,
                  names = TRUE,
                  editableNames = TRUE
                ),
                class = "numeric")
  })
}

shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.