首页 > 解决方案 > 不返回 R 中的绘图和表格闪亮

问题描述

我正在做一个闪亮的项目,以获得 2019/2020 年英超联赛目标的仪表板。我已经抓取了我的数据并使用以下代码尝试制作一个漂亮的仪表板。问题是我的“情节”和“表格”没有在应用程序中返回。它只给出列而不是表格和绘图的内容。有人可以帮助我或看到我做错了什么?

library(httr)
library(tidyverse)
library(dplyr)
library(rvest)
library(ggplot2)
library(shiny)
library(shinythemes)

Top_goalscorers <- Top_goalscorers

dput(Top_goalscorers)

ui <- fluidPage(
  titlePanel("Premier league goals 2019/2020"),
  sidebarLayout(
    sidebarPanel(
      selectInput("typeInput", "Club",
                  choices = c("Arsenal", "Liverpool", "Chelsea", "Leicester City", "Southampton","Manchester City", 
                              "Tottenham Hotspur", "Manchester United","Wolverhampton Wanderers","Chelsea","Burnley",
                              "Everton","Norwich City","Watford","West Ham United","Brighton & Hove Albion",
                              "Crystal Palace","Aston Villa","AFC Bournemouth","Sheffield United","Newcastle United"), 
                  selected = "Arsenal"),
      sliderInput("goalInput", "Goals", 0,30, c(0,30)),
      radioButtons("spelerInput", "Speler",
                   choices = c("Jamie Vardy", "Harry Kane"), 
                   selected = "Jamie Vardy")
    ),
    mainPanel(
      tabsetPanel(
                  tabPanel ("plot", plotOutput("myplot")),
                  tabPanel ("tabel", tableOutput("resultaten"))
    )
    )
  )
)

server <- function(input, output) {
  
  output$resultaten <- renderTable({
    filtered <-
      Top_goalscorers %>%
      filter(club == input$typeInput,
             goals >= input$goalInput,
             speler == input$spelerInput)
    filtered
  })
  output$myplot <- renderPlot({
    filtered <-
      Top_goalscorers %>%
      filter(club == input$typeInput,
             goals >= input$goalInput[1],
             goals <= input$goalInput[2],
             speler == input$spelerInput
      )
    ggplot(filtered, aes(x=goals, y=club, fill=goals_eerste_helft)) +
      geom_point(size=2, shape=23)
    
    
  })
  
}

shinyApp(ui = ui, server = server)

这里是数据集的结构:

数据集 Top_goalscorers

标签: rshiny

解决方案


一次将过滤后的数据创建为反应对象,并在表格和绘图中使用它。尝试这个

server <- function(input, output, session) {
  filtered <- reactive({
    req(input$typeInput,input$goalInput[1],input$goalInput[2],input$spelerInput)
    Top_goalscorers %>%
    filter(club == input$typeInput,
           goals >= input$goalInput[1],
           goals <= input$goalInput[2],
           speler == input$spelerInput
    )})
  
  output$resultaten <- renderTable({
    filtered()
  })
  
  output$myplot <- renderPlot({
    ggplot(filtered(), aes(x=goals, y=club, fill=goals_eerste_helft)) +
      geom_point(size=2, shape=23)
  })
  
}

推荐阅读