This is a simplified and clearer version of my previous post "In R/Shiny, how do I use Action Button to trigger Observe Event function?"
The below code outputs a plot of user inputs, on a Reactive basis, so the user can watch his build of inputs. The plot needs to remain reactive (not linked to the click of the Action Button). The Observe Event in the below code creates an R object out of those inputs (called "matrix_inputs.R"), for use in another R function not shown here. I would like to have the user save that input data into the R object (trigger the Observe Event) only when the user clicks the Action Button. The user could then continue inputting more data into the input matrix and watch the plot change on a reactive basis, without changing that R object. If the user clicks the action button again after making these further changes, that updated data would then be saved and written over that same R object created in Observe Event.
I understand I should insert "input$go, {...}" into the Observe Event. It´s not in the below code because the App will not work if I insert it, it immediately crashes. I have tried various iterations with no luck. Anyone have any insights on how to make this work? Keeping the plot completely real-time reactive, while linking the Observe Event to the Action Button.
Here´s the code:
library(shiny)
library(shinyMatrix)
m <- matrix(c(1,1), 1, 2, dimnames = list(NULL, c("Y", "Z")))
ui <- fluidPage(
titlePanel("Vector Generator"),
sidebarPanel(
width = 6,
tags$h5(strong("Matrix inputs:")),
matrixInput("matrix_input",
value = m,
rows = list(extend = TRUE, names = TRUE, editableNames = TRUE),
cols = list(extend = FALSE, names = TRUE, editableNames = FALSE),
class = "numeric")
), # closes sidebarPanel
actionButton(inputId = "go",label = "Save data"),
mainPanel(
h5(strong("Plot of matrix inputs:")),
width = 6,plotOutput("graph"),
h5(strong("Table of matrix inputs:")),
tableOutput("view"),
) # closes main panel
) # closes user input (UI) section
server <- function(input, output, session) {
matrix_input <- reactive(input$matrix_input)
# Captures input data for further processing in R ----
observeEvent(matrix_input(), {matrix_input.R <<- unique(matrix_input())})
output$graph <- renderPlot({
plot(matrix_input(),type="b")
})
output$view <- renderTable({
matrix_input()
})
} # closes user server section
shinyApp(ui, server)