4

I am creating a shiny app which will generate the score for clients based on their different features. In my shiny app, I have provided the checkboxGroupInput to select the required features. Based on the selected features app will dynamically add numericInput to the web ui so that user can assign the weight for the selected features and further the weights will be used in score calculation. I have tried different ways to get the selected features and wanted to save them in a vector. So that I can use the elements of the vector to calculate the score. Please someone show me a solution to save the selected features from checkboxGroupInput in a vector and based on that vector access the dynamically created numbericInput values.

Code Snippet

# Select variables to determine the credit worthyness
    checkboxGroupInput(

      inputId = "selected_var",

      label = "Choose variables:",

      choices = c(
        "R" = "r",
        "F" = "f",
        "M" = "m"
        ),

      selected = c("r","f"))
  )

server <- function(input, output) {

  output$weights_input <- renderUI({ 

req(input$selected_var)
req(input$weights)


lapply(1:length(input$selected_var), function(i) {
  numericInput(inputId = paste0(input$selected_var[i],"_weight"), label = input$selected_var[i], min = 0, max = 1, value = 0)
})
  })
Florian
  • 24,425
  • 4
  • 49
  • 80
Venkiii
  • 57
  • 1
  • 7

1 Answers1

5

You can get the value of a dynamically generated input as

input[[paste0(input$selected_var[i],"_weight")]]`

while you can get the array with selected check boxes simply with input$selected_var.

A working example is given below, I hope this helps!

library(shiny)

ui <- fluidPage(
  checkboxGroupInput(
    inputId = "selected_var",
    label = "Choose variables:",
    choices = c(
      "R" = "r",
      "F" = "f",
      "M" = "m"
    ),
    selected = c("r","f")
  ),
  uiOutput('weights_input'),
  textOutput('score')
)

server <- function(input, output) {

  output$weights_input <- renderUI({ 
    req(input$selected_var)
    lapply(1:length(input$selected_var), function(i) {
      numericInput(inputId = paste0(input$selected_var[i],"_weight"), label = input$selected_var[i], min = 0, max = 1, value = 0)
    })
  })

    output$score <- renderText({
      req(input$selected_var)
      selected = input$selected_var
      values = sapply(1:length(input$selected_var), function(i) {
        req(input[[ paste0(input$selected_var[i],"_weight")]]);input[[ paste0(input$selected_var[i],"_weight")]]
      })
      values = setNames(values,selected)
      paste0('Input: [', paste(names(values), values, sep = ":", collapse = ", "), ']. The sum of the values is ', sum(values))

  })
}

shinyApp(ui,server)
Florian
  • 24,425
  • 4
  • 49
  • 80
  • @Venkiii, glad I could help! Please consider [accepting the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if it solved your issue to mark your question as resolved. – Florian Jun 11 '18 at 12:46
  • If I print the `selected = input$selected_var ; selected` to ui, it is showing only one element (the first one) from the input$selected_var array. how to save all the elements of input$selected_var. Please let me know. I tried in different ways but couldn't succeeded. – Venkiii Jun 11 '18 at 12:49
  • If you add `print(selected)` it will print both `r` and `f` on startup. – Florian Jun 11 '18 at 12:51
  • please let me know how to save input$selected_var into a particular vector – Venkiii Jun 11 '18 at 12:53
  • I am not sure what you are looking for, it is already a vector called `selected` in my example code. If you want to it be reactive, see [here](https://shiny.rstudio.com/reference/shiny/1.0.2/reactiveVal.html). – Florian Jun 11 '18 at 12:59
  • I would like to specially thank you for your solution. It made my day. There is something, I want to get you see something with `print(selected)`. If you render `print(selected)`, it is only showing first element from the _**selected**_ not all the elements. Please throw some light on this. – Venkiii Jun 12 '18 at 06:26
  • @Venkiii if you actually render it, you have to specify how you want it printed, otherwise it will just render the first element. Try `paste(selected, collapse = '')` – Florian Jun 12 '18 at 07:13