0

I am trying to access output values inside the server on my shiny app. The values are inputed by the user in a loop: depending on the number 'n_matrizes', the user inputs 'n_matrizes' number of matrices. The server then collects all these matrices in a list, using lapply:

lapply(1:(n_matrizes()),function(i){
             matrixInput(inputId = paste0("W_",i),
                         label = paste("Matriz espacial de ordem", i),
                         value = matrix(0,nrow=n_radares(),ncol=n_radares()))})

In the server, I need to access each of these matrices on the list. I've tried using reactive and reactiveValues but since I'm new to this programming, I haven't succeeded.

On normal R coding, I have done this before:

To create the list of matrices:

w_1 <- matrix(1,nrow=4,ncol=4)
w_2 <- matrix(2,nrow=4,ncol=4
w_3 <- matrix(3,nrow=4,ncol=4
W <- list(w_1,w_2,w_3)

To access one of them:

W[[1]]

How do I do that on the shiny server?

  • 1
    Welcome to SO! In general, you can access the matrices with `input$W_1` etc. Because they are reactive values, you can only access them in a reactive context (e.g. `reactive`, `observeEvent`). Can you elaborate a bit on your usecase and maybe give some (mock) code what you want to do? – starja Feb 05 '21 at 21:25

1 Answers1

0

You can access any input element within the server using it's inputId. If the inputId is a combination of strings and numbers you already know to use paste(). Let us say you have a list of widgets with a name pattern W_1, W_2, W_3 ... you can access them easily with input[[paste0("W_", 1L)]]. Of course, you can replace the number by any integer variable.

From your sample I assume that you have a function that creates several instances of inputMatrix. Your code also suggests that when I call that function repeatedly, I get multiple instances with the same inputId W_1, W_2, .... That will cause an error. Make sure your inputIds are unique.

Jan
  • 4,974
  • 3
  • 26
  • 43