首页 > 解决方案 > R shinyApp 应用程序有效,但在我的 . 需要简化

问题描述

此处提供完整代码:https://github.com/donwillmar/dataviz/blob/master/Evomass_RUG(app.R)

我做了一个函数shinyApp,可以比较一个数据集中的 3 个指标在橄榄球队中的位置。

在数据集中,观察结果如下所示:

| Year | Name | Nation |   Zone    | KG | CM  | BMI |
| 2019 |  Bob |  USA   |  Flyhalf  | 80 | 183 | 23  |

有 7 个不同的职位~Zone,我在各个指标KG中分别CM进行了比较。总共有 111 个地块,我将它们保存为一个名为“地块”的列表。BMIggplots

ui <- fluidPage(
theme = shinythemes::shinytheme("superhero"),
titlePanel("Mass Evolution of Rugby Internationals"),
sidebarPanel(position = "left",
             wellPanel(h3("Variables"),
                       radioButtons(inputId = "yvar", label = h4("Mass Metric"), 
                                    choices = list("Body Mass Index" = "BMI", "Weight (KG)" = "KG","Height (CM)" = "CM"),
                                    selected = "BMI"),

                       selectInput(inputId = "zone", label = h4("Position Zone"), 
                                   choices = list("Front Row", "Second Row", "Back Row",
                                                  "Scrumhalf", "Flyhalf", "Centre", "Outside Back"),
                                   selected = "Front Row"),

                       selectInput(inputId = "comp", label = h4("Compare Position"), 
                                   choices = list("None", "Front Row", "Second Row", "Back Row",
                                                  "Scrumhalf", "Flyhalf", "Centre", "Outside Back"),
                                   selected = "None")

在函数中server.R,我根据用户的ifelseggplotplotsinput$zoneinput$compare

server <- function(input, output) {

selectedData <- reactive({
    c(input$yvar, input$zone, input$comp)
})

output$graf <- renderPlot ({

        if ((input$yvar == "BMI") & (input$zone == "Front Row") & (input$comp == "None")){  
        plots[[1]]
    } else if ((input$yvar == "BMI") & (input$zone == "Front Row") & (input$comp == "Front Row")){  
        plots[[1]]
    } else if ((input$yvar == "BMI") & (input$zone == "Front Row") & (input$comp == "Second Row")) {
        plots[[28]]
    } else if ((input$yvar == "BMI") & (input$zone == "Front Row") & (input$comp == "Back Row")) {
        plots[[29]]

...这些陈述中有 111 条。

我对这个应用程序失去了动力,因为我想向它添加更多内容,而且我知道有一种更简单的方法可以将它与switch功能或其他东西一起使用,但我似乎无法弄清楚。

标签: rif-statementshinysimplify

解决方案


推荐阅读