Home > Blockchain >  In R shiny, how to generate sequential column headers for an input matrix?
In R shiny, how to generate sequential column headers for an input matrix?

Time:09-30

When running the below MWE code, the user can add, delete, or modify entries into the input matrix grid. A simple plot is generated from those user inputs.

You'll see default column headers of 1 and 2 for the input matrix. However, when the user adds data to the matrix (by simply clicking on an empty column and inputting a value), how can a sequential column header by automatically generated for that new column? So an added 3rd column would have header label of 3, etc. Column deletion (by clicking the "x" that currently appears in the column header field) would cause new sequential column numbering to be generated for remaining columns.

Note that by changing the matrixInput column (col) specification below to editableNames = TRUE, the user is then able to manually input a column header. But it would be far better if numbered headers were automatically generated and editableNames were set at FALSE. Those column header numbers will be used in other calculations.

MWE code:

library(shiny) 
library(shinyMatrix) 

m <- matrix(runif(2), 1, 2, dimnames = list(c("Sample values"), c(1,2))) 

ui <- fluidPage(   
  titlePanel("Matrix inputs"),   
  sidebarPanel(     
    width = 6,     
    matrixInput(       
      "sample",       
      value = m,       
      rows = list(extend = FALSE),       
      cols = list(extend = TRUE, names = TRUE, editableNames = FALSE, delete = TRUE),
      class = "numeric"
      )   
    ),   
  mainPanel(     
    width = 6,     
    plotOutput("scatter")   
    ) 
  ) 

server <- function(input, output, session) {   
  
   output$scatter <- renderPlot({
     plot(1:ncol(input$sample),
          input$sample,
          xlab="Nbr of samples",
          ylab="Sample values"
          )
     }) 
   
   } 
shinyApp(ui, server) 

CodePudding user response:

  1. observe changes in the input matrix in the server.
  2. Change the colnames of the matrix given by input$sample. I simply used consecutive numbers here but you can provide any algorithm you want.
  3. Send the new matrix back to the UI with updateMatrixInput. Make sure you use isolate to avoid an endless cycle of change and refresh.
library(shiny) 
library(shinyMatrix) 

initm <- matrix(runif(2), 1, 2, dimnames = list(c("Sample values"), c(1,2)))

ui <- fluidPage(   
  titlePanel("Matrix inputs"),   
  sidebarPanel(     
    width = 6,     
    matrixInput(       
      "sample",       
      value = initm,       
      rows = list(extend = FALSE),       
      cols = list(extend = TRUE, names = TRUE, editableNames = FALSE, delete = TRUE),
      class = "numeric"
    )   
  ),   
  mainPanel(     
    width = 6,     
    plotOutput("scatter")   
  ) 
) 

server <- function(input, output, session) {
  observe({
    mm <- input$sample
    colnames(mm) <- 1:ncol(mm)
    isolate(
      updateMatrixInput(session, "sample", mm)
    )
  })
  
  output$scatter <- renderPlot({
    plot(1:ncol(input$sample),
         input$sample,
         xlab="Nbr of samples",
         ylab="Sample values"
    )
  }) 
  
} 
shinyApp(ui, server) 
  • Related