Reputation: 2720
In the below MWE code, I'm trying to insert a reactive value "periods" (generated from the slider input and as defined in the Server section) into the first row and first column of a user-input matrix that appears in the UI section. As you can see below, current setting for that first row/column of matrix is "1". I'd like it to instead be the same value as in "periods", and vary reactively with changes in periods effected by the user moving the slider at the top.
I'm sure this requires some proficiency with Reactivity/observeEvent that I don't yet have. I have fooled with these parameters to no avail.
Also see images below to better explain.
library(shiny)
library(shinyMatrix)
ui <- fluidPage(style = "margin-top:10px;",
column(3,
fluidRow(
sliderInput("periods", "Nbr of periods (X):",min = 1, max = 120, value = 60)),
fluidRow(
matrixInput(
"vector_input",
label = "Generate matrix:",
value = matrix(c(1, # << this 1 value needs to be reactive input$periods value
0.2),
1, 2, dimnames = list(NULL, c("Y", "Z"))),
rows = list(extend = TRUE, names = FALSE),
cols = list(extend = FALSE, names = TRUE, editableNames = FALSE),
class = "numeric"),
textOutput("selected_var")
)),
column(9,))
server <- function(input, output) {
output$selected_var <- renderText({paste("Number of periods: ", input$periods)})
vals <- reactiveValues()
observe({vals$periods <- input$periods})
vector_input <- reactive(input$vector_input)
} # close server
shinyApp(ui = ui, server = server)
This is what appears when running the code as drafted and what I am trying to do. In the second image I show the user changing the periods input from 60 to 20, and how I would like the 1st row in column Y of the matrix to reflect that change:
Upvotes: 0
Views: 331
Reputation: 12461
The updateMatrixInput
function gives you a simple solution. Replace your current server function with
server <- function(input, output, session) {
output$selected_var <- renderText({paste("Number of periods: ", input$periods)})
observeEvent(input$periods, {
updateMatrixInput(session, "vector_input", value=matrix(c(input$periods, 0.2), 1, 2))
})
}
And I believe you get the functionality you want. Note the addition of the session
parameter to the definition of the server
function.
Upvotes: 1