R根据输入闪亮加分

R shiny addition of points according to input

我想创建一个输入与分数相匹配的应用程序。这是我的代码:

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)

任何人都可以帮助实现如何将分数添加到总分的功能吗?总分最高为 15 分。非常感谢。

试试这个简短而整洁的服务器表达式:

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)