OCO
OCO

Reputation: 41

r shiny - How to display a variable number of numeric inputs

I would like to display a variable number of inputs based on a numeric input "number of lines". As there can be potentially a large number of lines, I don't want to use a conditional panel for each line and would rather iterate with functions as below:

ui <- fluidPage(
  lapply(1:3,function(iter1){
    fluidRow(
      column(2,
             h4(paste0("line", iter1))
      ),
      lapply(1:4,function(iter2){
        column(2,
               h4(paste0("Wind speed bin", iter2)),
               column(6, numericInput(paste0("v", iter1,"bin",iter2,"min"),"Vmin",5)),
               column(6, numericInput(paste0("v", iter1, "bin",iter2,"max"),"Vmax",6))
        )
      })
    )
  })  
)

The code above allows to display 4 wind speed bins definitions per line j (numericInput: vjbin1min, vjbin1max, vjbin2min, vjbin2max, ...) on 3 lines.

Overview of sidebar layout

I would like now to define the number of bins [of lines] with a variable bin_nb [line_nb] as below:

lapply(1:bin_nb,function(iter1){...
lapply(1:line_nb,function(iter2){...

With two numeric inputs

numericInput("bin_nb","number of wind speed bins",value=4),    
numericInput("line_nb","number of lines",value=3)

I have tried to get the value from server side

output$bin_nb <- renderText(input$bin_nb)

And use

lapply(1:textOutput("bin_nb"),function(iter1){...

Which is of course not working as a numeric value is requested. How could I define and use these two numeric variables bin_nb and line_nb?

Upvotes: 1

Views: 737

Answers (1)

OCO
OCO

Reputation: 41

Based on BigDataScientist's comment, here is the detailed solution:

ui <- fluidPage(
  numericInput("line_nb","number of lines",value=3),
  numericInput("bin_nb","number of wind speed bins",value=4),
  uiOutput("input_panel")
)

And server side:

server <- function(input, output) {
  output$input_panel <- renderUI({

    lapply(1:input$line_nb,function(iter1){
      fluidRow(
        column(2,
               h4(paste0("line", iter1))
        ),
        lapply(1:input$bin_nb,function(iter2){
          column(2,
                 h4(paste0("Wind speed bin", iter2)),
                 column(6, numericInput(paste0("v", iter1,"bin",iter2,"min"),"Vmin",5)),
                 column(6,numericInput(paste0("v", iter1, "bin",iter2,"max"),"Vmax",6))
          )
        })
      )
    })
  })    
}

Upvotes: 2

Related Questions