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:
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.
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?
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?
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)