Search code examples
shinyaddition

R shiny addition of points according to input


I want to create an application where inputs are matched with a score. Here is my code:

library (shiny)
library(shinyWidgets)
library(fresh)
library(htmltools)
ui<-navbarPage("Addition of points",
       tabPanel("Calculation",
                
                fluidPage(                            
                  headerPanel(h4("Scores")), 
                  headerPanel(h5("Please select your inputs.")),
                  
                  fluidRow(
                    (column (3, 
                             
                             headerPanel(h5(strong("Score1"))),
                             
                             sliderInput("score1", label = h6("Score1 (1-3), input <=2 should add 6 points to the total score, input >2 should add no points"), min = 1, max = 3, value = 1, ticks = TRUE, animate=TRUE, step = 1, width = "150px")                                      
                             
                    )),
                    (column (3,
                             
                             headerPanel(h5(strong("Score2"))),
                             
                             numericInput("score2", label = h6("Score2, input <=10 should add 5 points to the total score, input >10 should add no points"), value = "1" , width = "150px")
                             
                    )),
                    (column (3,
                             headerPanel(h5(strong("Score3"))),
                             radioButtons("score3", label = h6("Score3, clicking on 'No' should add 4 points to the total score, clicking on 'Yes' should add no points"), choices = list("No"=1, "Yes"=2), selected = character(0))
                             
                             
                    )),
                    (column (2,                                         
                             wellPanel(
                               style = "background: white", 
                               headerPanel(h4(strong("Score:"))),                                             
                               textOutput("multi")                                             
                             )))
                  )))
)
server<-function(input, output) {
    B=function(x, y){
        if(input$score1<=2) {+6}
        if(input$score2<=10) {+5}
        if(input$score3==1) {+4}
    }
    output$multi <- renderText ({ B() })
}
shinyApp(ui, server)

Can anyone help with the function how to add the points to the total score? The maximum total score would be 15 points. Thank you so much.


Solution

  • Try this short and tidy server expression:

    library (shiny)
    library(shinyWidgets)
    library(fresh)
    library(htmltools)
    ui<-navbarPage("Addition of points",
           tabPanel("Calculation",
                    
                    fluidPage(                            
                      headerPanel(h4("Scores")), 
                      headerPanel(h5("Please select your inputs.")),
                      
                      fluidRow(
                        (column (3, 
                                 
                                 headerPanel(h5(strong("Score1"))),
                                 
                                 sliderInput("score1", label = h6("Score1 (1-3), input <=2 should add 6 points to the total score, input >2 should add no points"), min = 1, max = 3, value = 1, ticks = TRUE, animate=TRUE, step = 1, width = "150px")                                      
                                 
                        )),
                        (column (3,
                                 
                                 headerPanel(h5(strong("Score2"))),
                                 
                                 numericInput("score2", label = h6("Score2, input <=10 should add 5 points to the total score, input >10 should add no points"), value = "1" , width = "150px")
                                 
                        )),
                        (column (3,
                                 headerPanel(h5(strong("Score3"))),
                                 radioButtons("score3", label = h6("Score3, clicking on 'No' should add 4 points to the total score, clicking on 'Yes' should add no points"), choices = list("No"=1, "Yes"=2), selected = character(0))
                                 
                                 
                        )),
                        (column (2,                                         
                                 wellPanel(
                                   style = "background: white", 
                                   headerPanel(h4(strong("Score:"))),                                             
                                   textOutput("multi")                                             
                                 )))
                      )))
    )
    server<-function(input, output) {
        B <- reactive({
            0 + 
                (if(!is.null(input$score1) && input$score1<=2) 6 else 0) + 
                (if(!is.null(input$score2) && input$score2<=10) 5 else 0) +
                (if(!is.null(input$score3) && input$score3==1) 4 else 0)
        })
        output$multi <- renderText ({ B() })
    }
    shinyApp(ui, server)