shiny - R根据输入 Shiny 添加点

标签 shiny addition

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

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)

关于shiny - R根据输入 Shiny 添加点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70855454/

相关文章:

c++ - 组合 ECDSA key

Rselenium 无法连接到正在运行的 Shiny 应用程序

javascript - 使用 Javascript API 将 Tableau 集成到 Shiny 项目中

r - 错误 : [on_request_read] connection reset by peer in R shiny

R Shiny 将分隔符添加到单选按钮列表选项中

javascript - 向对象添加新值

ios - 当用户在我的应用程序中添加卡路里时,如何使标签增加

C# 添加可空小数和 ?? 的优先级运算符(operator)

jquery - 使用 jquery 添加链接的 href 值

r - 在 shinyapps.io 中部署 R 应用程序 - "Error:parsing manifest: Manifest file manifest.json checksum mismatch"