为什么我的过滤器功能在rShiny中不起作用?

kmbjn2e3  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(148)

每次我尝试运行rShiny应用程序时,它都不会给我一个错误,但我的图不会一直显示。我知道这与我的过滤器有关,因为我添加了一个输出来查看我正在过滤的表,它是空的。
它显示了边框和一些标签,但是图形本身没有显示,还有一个空表。This是我的webapp的样子。
我的代码:

library(shiny)
library(ggplot2)
library(dplyr)

ui = fluidPage(
  titlePanel('NFL Draft'),
  
  sidebarLayout(
    
    sidebarPanel(
      
      checkboxGroupInput('seasons', 'Seasons:',
                         choices = c('2004' = 1,
                                     '2005' = 2,
                                     '2006' = 3,
                                     '2007' = 4,
                                     '2008' = 5,
                                     '2009' = 6,
                                     '2010' = 7,
                                     '2011' = 8,
                                     '2012' = 9,
                                     '2013' = 10,
                                     '2014' = 11,
                                     '2015' = 12,
                                     '2016' = 13),
                         selected = c(1,2,3,4,5)),
      checkboxGroupInput('positions', 'Positions:',
                         choices = c('QB' = 1,
                                     'TE' = 2,
                                     'WR' = 3,
                                     'RB' = 4,
                                     'FB' = 5),
                         selected = 1)),
    mainPanel(
      
      plotOutput('epa_by_draft'),
      tableOutput('table')
    )
  )
)

server <- function(input, output) {
  
  position.x = c('QB', 'QB', 'QB', 'RB', 'RB', 'WR', 'WR', 'TE', 'FB', 'FB', 'WR', 'TE', 'TE', 'QB', 'QB')
  season.y = c(2012, 2010, 2004, 2004, 2010, 2005, 2007, 2006, 2008, 2009, 2011, 2014, 2013, 2015, 2016)
  round = c(1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1)
  total_epa = c(-3, -2, -1, 0, 1, 2, 3, -3, -2, -1, 0, 1, 2, 3, 0)
  draft_epa = data.frame(position.x, season.y, round, total_epa)
  filtered_draft_epa = reactive({draft_epa %>%
      filter(position.x %in% input$positions & season.y %in% input$seasons)})
  
  output$table = renderTable({
    print(filtered_draft_epa())
  })
  
  output$epa_by_draft = renderPlot({
    ggplot(filtered_draft_epa(), aes(x = season.y,
                          y = round,
                          fill = total_epa))+geom_tile()+
    scale_fill_gradient(high = 'green', low = 'red', limits = c(floor(-3), ceiling(3)))
  })
}

shinyApp(ui = ui, server = server)

我正在尝试创建一个带有两个复选框过滤器小部件的rShiny应用程序,允许用户为"EPA by draft round"热图选择年份或位置的任意组合,热图应该显示在主面板中。
而不是在我运行代码时给我这个,我得到了除了热图的绘制部分之外的所有东西,这与我的过滤函数有关。
编辑:更新代码,使其可重现

jjjwad0x

jjjwad0x1#

问题是,您将choices设置为数字1、2、3 ......,而您数据中的类别是年份(即20082009 ......)和球员位置(即"QB""WR" ......)。因此,过滤后总是得到一个空数据集。只需将数据中的类别用于choices参数即可解决此问题:

library(shiny)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  titlePanel("NFL Draft"),
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("seasons", "Seasons:",
        choices = 2004:2016,
        selected = 2004:2008
      ),
      checkboxGroupInput("positions", "Positions:",
        choices = c(
          "QB",
          "TE",
          "WR",
          "RB",
          "FB"
        ),
        selected = c("QB", "WR")
      )
    ),
    mainPanel(
      plotOutput("epa_by_draft"),
      tableOutput("table")
    )
  )
)

server <- function(input, output) {
  position.x <- c("QB", "QB", "QB", "RB", "RB", "WR", "WR", "TE", "FB", "FB", "WR", "TE", "TE", "QB", "QB")
  season.y <- c(2012, 2010, 2004, 2004, 2010, 2005, 2007, 2006, 2008, 2009, 2011, 2014, 2013, 2015, 2016)
  round <- c(1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1)
  total_epa <- c(-3, -2, -1, 0, 1, 2, 3, -3, -2, -1, 0, 1, 2, 3, 0)
  draft_epa <- data.frame(position.x, season.y, round, total_epa)
  filtered_draft_epa <- reactive({
    draft_epa %>%
      filter(position.x %in% input$positions & season.y %in% input$seasons)
  })

  output$table <- renderTable({
    print(filtered_draft_epa())
  })

  output$epa_by_draft <- renderPlot({
    ggplot(filtered_draft_epa(), aes(
      x = season.y,
      y = round,
      fill = total_epa
    )) +
      geom_tile() +
      scale_fill_gradient(high = "green", low = "red", limits = c(floor(-3), ceiling(3)))
  })
}

shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:7087
#>   position.x season.y round total_epa
#> 1         QB     2004     3        -1
#> 2         WR     2005     6         2
#> 3         WR     2007     7         3

相关问题