简体   繁体   中英

Arranging Input Boxes (selectInput / numericInput etc…) dynamically in columns in R shiny

I am taking significant variables from ML model(.rda file),putting them as numericInput box or selectInput Option menu in the UI page. I am getting each Input in different row. I want it as two columns in a row. How can I do that? I am getting it as shown in the image

Below is the code which I want to modify.

 model <- reactive({readRDS(input$Load_model$datapath)})
  temp_col <- reactive({colnames(model()$model)})
  temp_no_col <- reactive({ncol(model()$model)})

  abc <- reactive(lapply(model()$model, class))
  process <- eventReactive(input$show_fields, {
    lapply(1:(temp_no_col()), function(i) {
        if(abc()[i] == "numeric" ) {
          numericInput(temp_col()[i], label = temp_col()[i],value = 0)
        }
        else if(abc()[i] == "factor") {
          selectInput(temp_col()[i], label = temp_col()[i],choices = unique(model()$model[i]))
        }
    })
  })

As @ismirsehregal says in the comments, you need to take a look at the shiny layout guide. You are creating the inputs programmaticaly, with lapply , so you need to dispatch the inputs created into two columns, something like this:

process <- eventReactive(input$show_fields, {
  inputs_temp <- lapply(1:(temp_no_col()), function(i) {
    if(abc()[i] == "numeric" ) {
      numericInput(temp_col()[i], label = temp_col()[i],value = 0)
    }
    else if(abc()[i] == "factor") {
      selectInput(temp_col()[i], label = temp_col()[i],choices = unique(model()$model[i]))
    }
  })

  shiny::tagList(
    shiny::fluidRow(
      # odd cols column
      shiny::column(6, inputs_temp[1:length(inputs_temp) %% 2 != 0]),
      # even cols column
      shiny::column(6, inputs_temp[1:length(inputs_temp) %% 2 == 0])
    )
  )
}

But note that you didn't provide a reproducible example , so I couldn't test the accuracy of the answer. If you edit the question with an small working example of your app, I can tweak and edit my answer to work with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM