0

I have two picker input selectors, each has 4 labels (low, medium, large, very large) attached to a value (1,2,3,4). Once the user has made his/her selection I want to display the overall average. For some reason, my code is calculating the average input without trouble, but it doesn't consider the second selector at all. Am I missing a parenthesis somewhere?

enter image description here

Ideally I'd like to show the corresponding label name for the average but that's secondary. Here is a reproducible example. Many thanks in advance!

'''
if (interactive()) {
  
  library(shiny)
  library(shinyWidgets)
  
  ui <- fluidPage(

    pickerInput(
      inputId = "groups_2",
      label = "Select two from each group below:",
      choices = c("Low" = "1", "Medium" = "2","Large" = "3", "Very Large" = "4"),
      multiple = TRUE,
      options =  list("max-options-group" = 2)
    ),
    verbatimTextOutput(outputId = "res_grp_2"),
    pickerInput(
      inputId = "classic",
      label = "Select max two option below:",
      choices = c("Low" = "1", "Medium" = "2","Large" = "3", "Very Large" = "4"),
      multiple = TRUE,
      options =  list(
        "max-options" = 2,
        "max-options-text" = "No more!"
      )
    ),
    verbatimTextOutput(outputId = "res_classic"),
    textOutput(outputId = "avg")
  )
  
  server <- function(input, output) {
    
    output$res_grp_2 <- renderPrint(input$groups_2)
    output$res_classic <- renderPrint(input$classic)
    avg<-reactive(mean(as.numeric(input$groups_2, input$classic)))
    output$avg<-renderText(avg())
    
  }
  
  shinyApp(ui, server)
  
}

'''

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Lea7885
  • 97
  • 1
  • 9
  • `as.numeric(input$groups_2, input$classic)` is the issue as each variable needs to be numeric. – YBS Mar 20 '21 at 12:14
  • Thanks for pointing that out @YBS, you are right if I don't use `as.numeric` I get an error because they are not numeric. I'm not able to figure out is to average both inputs, or should I actually write a function such as `(input$groups_2+ input$classic)/2` ? – Lea7885 Mar 20 '21 at 15:30

1 Answers1

1

As you can have different number of elements selected in each pickerInput, it is better to calculate your own average. One way to do it is shown below.

library(shiny)
library(shinyWidgets)

ui <- fluidPage(

  pickerInput(
    inputId = "groups_2",
    label = "Select two from each group below:",
    choices = c("Low" = "1", "Medium" = "2","Large" = "3", "Very Large" = "4"),
    multiple = TRUE,
    options =  list("max-options" = 2)
  ),
  verbatimTextOutput(outputId = "res_grp_2"),
  pickerInput(
    inputId = "classic",
    label = "Select max two option below:",
    choices = c("Low" = "1", "Medium" = "2","Large" = "3", "Very Large" = "4"),
    multiple = TRUE,
    options =  list(
      "max-options" = 2,
      "max-options-text" = "No more!"
    )
  ),
  verbatimTextOutput(outputId = "res_classic"),
  textOutput(outputId = "avg")
)

server <- function(input, output) {

  output$res_grp_2 <- renderPrint(input$groups_2)
  output$res_classic <- renderPrint(input$classic)
  avg  <-  reactive({
    req(input$classic,input$groups_2)
    numr <- sum(sum(as.numeric(input$groups_2)), sum(as.numeric(input$classic)))
    denom <- sum(length(input$groups_2),length(input$classic))
    avg <- numr/denom
  })
  output$avg<-renderText(avg())
}

shinyApp(ui, server)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • I wouldn't have been able to come up with the correct syntax for this. Many thanks @YBS, it works perfectly! – Lea7885 Mar 20 '21 at 17:22