1

I´m new to shiny and shinyMatrix, though I´ve gone through tutorials. The below code conceptually gets me what I need. When running, you add to the input matrix by clicking on an empty cell at the bottom of the grid and key in a value, and can essentially make the matrix as large as you would like. By hitting the "Update" button the plot is updated with the additional data. Very nice.

My questions are:

  1. How do you create a data frame or equivalent out of the updated matrix inputs? For further use in R. As currently coded, the "m" matrix only contains the initial 2 values and not the additional grid inputs.

  2. When running, to get matrix insertions to work properly, I have to enter a grid value into the empty cell in column "y" first, and then column "x", to the left, next. If I insert a value into column "x" first, usually the "y" column is blocked from input. Any way to fix this?

  3. When running, note the empty column to the left. I´d like only 2 columns, for x and y. How do I eliminate that 1st empty column?

  4. Is there a better way to do this? The input matrix needs to be variable in concept, like the below. I need to be able to access those matrix inputs in a data frame for further use.

Code (source is https://www.r-bloggers.com/2020/02/shinymatrix-matrix-input-for-shiny-apps/):

library(shiny)
library(shinyMatrix)

m <- matrix(runif(2), 1, 2, dimnames = list(NULL, c("x", "y"))) 
ui <- fluidPage(   
  titlePanel("shinyMatrix: Demo"),   
  sidebarPanel(     
    width = 6,     
    tags$h4("Data"),     
    matrixInput("sample",value = m,rows = list(extend = TRUE),cols = list(names = TRUE))
  ),
  actionButton(inputId = "go",
               label = "Update matrix"),
  mainPanel(     
    width = 6,plotOutput("scatter")) 
) 

server <- function(input, output, session) {   
  output$scatter <- renderPlot({     
    plot(input$sample, col = "red", main = "Scatterplot")}) 
} 

shinyApp(ui, server)

Oops, I omitted the eventReactive function in the above! Reposting the code (though it does not impact my question or any answer) for the sake of completeness:

m <- matrix(c(1,1), 1, 2, dimnames = list(NULL, c("x", "y"))) 

ui <- fluidPage(   
  
  titlePanel("shinyMatrix: Demo"),   
  
  sidebarPanel(
    
    # Input: specify the number of observations to view ----
    sliderInput("integer", "Number of periods to spread data inputs along:",
                min = 1, max = 120, value = 60),
    
    # Input: matrix grid ----
    width = 6,     
    tags$h5(strong("Data inputs:")),     
    matrixInput("sample",value = m,
                rows = list(extend = TRUE),
                cols = list(names = TRUE))
  ),
  
  actionButton(inputId = "go",label = "Update below:"),
  
  mainPanel(
    
    # Show input graph  ----  
    h5(strong("Plot of inputs:")),
    width = 6,plotOutput("scatter"),
    
    # Show table of inputs, to be replaced with interpolated matrix ----    
    h5(strong("Table of inputs:")),
    tableOutput("view")
    
  ) 
) 

server <- function(input, output, session) {   
  
  data <- eventReactive(input$go,{input$sample}, ignoreNULL = FALSE)
  
  # Process output graph; type "l" for lines, "p" for points, "b" for combined ----  
  output$scatter <- renderPlot({     
    plot(data(), type="b", main = 'Variables over time', col = "blue", pch = 19, cex = 1.25)
  })
  
  # Process output table, to be replaced with interpolated matrix ----    
  output$view <- renderTable({
    data()
  })
  
} 

shinyApp(ui, server)

1 Answers1

0

OK so I did more research and solved this. Answering in the same order as the original questions:

  1. To create an R data frame (or vector) from the values of Shiny reactive data, you need to generate a "globally assigned vector" from that reactive data. To do this in this example, add the following line to the server section of the above code, below the first line under server that reads "data <- eventReactive(input$go,{input$sample}, ignoreNULL = FALSE)":

    observeEvent(data(), {m.update <<- unique(data())})

The reactive data is now captured in the "m.update" data frame in this example.

I got this solution from Storing a reactive output in a vector - Shiny R, with complete explanation

  1. The issue regarding inserting additional rows has been resolved. I posted the issue on GitHub and the developer quickly responded and resolved. No longer an issue with new version 0.6.1 of shinyMatrix

  2. Now that I know how to better use shinyMatrix I´m fine with the left-most column, used for labels. It´s easy to automatically generate names (labels) for that left column

  3. After doing much research, and with the above resolutions, shinyMatrix looks like the best solution for inputting multiple data fields into shiny reactive functions. I´m very happy with it!!