CnewbieWannabePro
CnewbieWannabePro

Reputation: 130

FInd the input ID of a textinput element dynamically

I have a simple server / ui program in R Shiny. With an lapply i have created -lets say- 10 textInputs.

The names of the inputs are marker1 marker2 marker3 etc.. with predefined values (no empty ones). I have created an observeEvent to check when the user presses the Submit button -created in the UI-, so that i can manipulate the new values.

When I do

     print(input$marker1)

it works fine and it shows the value of the specific marker.

The problem is, i want to do this for all the markers and the number of them is not static, so I came up with this idea, which doesnt work:

     for(i in length(markers))
     {
       markername = paste0("marker",i);
       print(input$markername)
     }

I understand the logic behind this is wrong, because after the input$ you need to put the actual name of the input, but how can I do that when the number of my markers is dynamic?

EDIT #1 AND UPDATE:

After looking around the input formats, i found this one, that actually lets you paste the name of the input:

      input[[paste0("name",i)]]

So, the problem now is something like this.

     for(i in length(markers))
     {
       global_var[i] <<- input[[paste0("name",i)]]
     }
     print(global_var)

The problem now is I get NULL for the first positions of the global_var object, except from the last one where the assignment is completed correctly.

Upvotes: 1

Views: 806

Answers (1)

CnewbieWannabePro
CnewbieWannabePro

Reputation: 130

The problem I encountered was as to how I can set the names of the inputs I want to show dynamically. As seen above, I tried different things but now I have come up with the whole solution, one that also gives you (if needed) the values of the said "grouped" inputs in a list.

I use a global var (because i want to use the values in other outputs as well) named newNames and initialized it as NULL in the beginning, the rest of the code is shown below, its a simple lapply:

      lapply(1:length(markers), function(i) 
      {
        newNames[i] <<- input[[paste0("marker", i)]]
      })
      printf(newNames)

Upvotes: 1

Related Questions